java - 向 JDialog 添加按钮?

标签 java swing jbutton jdialog

我正在尝试在 JDialog 上创建 JButton,但是该按钮将覆盖整个 JDialog,对此有什么帮助吗?这是它的样子:

enter image description here

这就是我创建 JDialog 和 JButton 的方法:

class MenuStoreHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = (int) dim.getWidth();
        int screenHeight = (int) dim.getHeight();

        JDialog g = new JDialog();
        g.setTitle("The Store");
        g.setSize(200, 200);
        g.setLocation(screenWidth / 2 - 150, screenHeight / 2 - 150);

        JButton b = new JButton("Buy");
        b.addActionListener( new StoreItem1Handler() );
        b.setVisible(true);
        g.add(b);

        g.setVisible(true);
    }
}

我将发布我的完整 MrStan.class,这里是:

package Progress;

public class MrStan extends JPanel{

    private Timer timer = new Timer();
    public static int points;
    static File h = new File("text.txt");
    public ImageIcon bg = new ImageIcon("D:/MrStan/bg.png");
    static JMenuBar menubar;
    Formatter x;
    JMenu menu;
    JMenuItem menuitem;

    double version = 0.3;

    class todoTask extends TimerTask{
        public void run(){ 
            points += 1;
            repaint();
        }
    }

    public int getPoints(){
        return points;
    }

    public void setPoints( int points ){
        this.points = points;
    }

    public MrStan(){
        setIgnoreRepaint(true);

        menubar = new JMenuBar();
        menu = new JMenu("Menu");
        menu.setMnemonic(KeyEvent.VK_F);
        menu.getAccessibleContext().setAccessibleDescription("Menu");
        menubar.add(menu);

        menuitem = new JMenuItem("Store (S)", new ImageIcon("coins.png"));
        menuitem.setMnemonic(KeyEvent.VK_S);
        menuitem.addActionListener( new MenuStoreHandler() );
        menu.add(menuitem);

        menuitem = new JMenuItem("Reset Points (R)", new ImageIcon("delete.png"));
        menuitem.setMnemonic(KeyEvent.VK_R);
        menuitem.addActionListener( new MenuResetPointHandler() );
        menu.add(menuitem);

        // add a separator
        menu.addSeparator();

        menuitem = new JMenuItem("Exit (E)", new ImageIcon("cross.png"));
        menuitem.setMnemonic(KeyEvent.VK_E);
        menuitem.addActionListener( new MenuExitHandler() );
        menu.add(menuitem);

        timer.schedule(new todoTask(), 0, 2000);

    }

    class MenuStoreHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){

            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            int screenWidth = (int) dim.getWidth();
            int screenHeight = (int) dim.getHeight();

            JDialog g = new JDialog();
            g.setTitle("The Store");
            g.setSize(200, 200);
            g.setLocation(screenWidth / 2 - 150, screenHeight / 2 - 150);

            JButton b = new JButton("Buy");
            b.addActionListener( new StoreItem1Handler() );
            b.setVisible(true);
            g.add(b);

            g.setVisible(true);
        }
    }

    class StoreItem1Handler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.out.println("Store-Button 1 pressed.");
        }
    }

    class MenuExitHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(1);
        }
    }

    class MenuResetPointHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            points = 0;
            repaint();
            JOptionPane.showMessageDialog(null, "Points have been reset.");
        }
    }

    public void paint(Graphics g){
        g.setColor(Color.WHITE);
        bg.paintIcon(this,g,0,0);
        g.setColor(Color.BLACK);
        g.drawString("Points: " + points, 75, 95);
        g.drawString("Version: " + version, 2, 10);
    }

    public static void main(String[] args){

        final MrStanCreateFile g = new MrStanCreateFile();

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
            public void run(){
                if(h.exists()){
                    g.openFile();
                    g.addRecords();
                    g.closeFile();
                }else{
                    System.out.println(h.getName() + "does not exist, not saving.");
                }
            }
        }, "Shutdown-thread"));

        readIt();

        //Create new JFrame
        JFrame frame = new JFrame();
        frame.setTitle("MrStan");
        frame.setSize(200, 200);
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setJMenuBar(menubar);

        //Set location of JFrame
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = (int) dim.getWidth();
        int screenHeight = (int) dim.getHeight();
        frame.setLocation(screenWidth / 2 - 200, screenHeight / 2 - 200);

        //Set ContentPane to JPanel
        MrStan panel = new MrStan();
        frame.setContentPane(panel);

        //Make the user not be able to resize
        frame.setResizable(false);

        //Make the JFrame visible
        frame.setVisible(true);
    }

    public static void readIt(){
        MrStanReadFile r = new MrStanReadFile();
        r.openFile();
        r.readFile();
        r.closeFile();
    }

}

为什么这会覆盖我的整个 JDialog?我正在使用基本的布局管理器,它应该没问题。

最佳答案

尝试先将按钮添加到 contentPane,然后再设置边界。

Container pane = dialog.getContentPane();
pane.setLayout(null);
JButton button = new JButton("Testbutton!");
pane.add(button);
button.setBounds(10,10,40,40);

关于java - 向 JDialog 添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837067/

相关文章:

java - 用另一个按钮替换一个按钮

java - 为什么静态final变量使用静态方法初始化需要synchronized?

java - 将字符串按条件传递给 SQL 语句

java - JButton 在单击时卡住(因为我在 ActionListener 中启动递归函数)

java - 如何设置已具有背景图像的 JButton 的大小?

java - 如何从另一种方法获取 JTextArea 文本

java - 并发 : Java Map

java - Android 上 java.awt.image.BufferedImage 的 ClassNotFoundException

java - 将事件监听器添加到组件并将该组件添加到面板或框架的顺序是什么?

java - JFrame isResizable(false) 尺寸问题