English 中文(简体)
添加纽芬兰语
原标题:Adding buttons to a JDialog?

I m trying to make a JButton on a JDialog, but, the button will cover the entire JDialog, any help on this? This is what it looks like:

“entergraph

这就是我如何创建正义与平等运动和联合邦:

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);
    }
}

我只是要把我的全副手登先生。 班次:

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();
    }

}

为什么这涉及我当时的IRE JDialog? 使用基本的Layout管理员的Im,这只是罚款。

最佳回答

先将 but子添加到内容Pane,然后确定约束。

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

Seems to workvy for me. 你们是否真的为dia疾呼?

This is what I tried

JDialog dialog = new JDialog();
dialog.setSize(300, 200);
dialog.setLayout(null);



JButton button = new JButton("Testbutton!");
button.setVisible(true);
button.setBounds(10,10,40,40);
dialog.add(button);

//Make dialog visible
dialog.setVisible(true);

And usually it s not a good practice to not use a layout manager. Things can get complicated very quickly. Layout Managers help a lot.

The real problem for you code is that you add the components to the dialog AFTER you set the dialog visible. The second setVisible() does nothing because its already visible.

因此,你应当学习Swaing tutorial的例子。 这些例子表明,你有适当的办法建立一个简单的全球倡议。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签