简体   繁体   English

Java摇摆按钮

[英]Java Swing Button

I've written this: 我写了这个:

JButton saveButton = new JButton(saveAction);

How do I then call it so that it displays within the window? 然后如何调用它以便使其在窗口中显示? (I've already got the code for the window, I just don't know how to call it so it shows) (我已经有了窗口的代码,我只是不知道如何调用它,所以它显示出来了)

saveButton.setVisible(true);

your_window.add(saveButton);

就这样。

Firstly you should create some ContentPane for window (I guess you mean JFrame). 首先,您应该为窗口创建一些ContentPane(我想您是说JFrame)。 Adding a button directly to window is not a good idea :P Next you can add your button to that pane: 直接将按钮添加到窗口不是一个好主意:P接下来,您可以将按钮添加到该窗格中:

panel.addComponent(button);

The last thing to do is: 最后要做的是:

frame.setContentPane(panel)

And that's all :P Just in a nutshell ;) 仅此而已:P简而言之;)

Use something like this 使用这样的东西

public class MainWindow extends JFrame {
    public static void main(String[] args) {
        MainWindow frame = new MainWindow();
        frame.setVisible(true);
    }

    public MainWindow() throws IOException {
        setTitle("Conveyor");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 851, 515);
        contentPane = new JPanel();
        JButton refreshButton = new JButton("refresh");
        contentPane.add(refreshButton, BorderLayout.EAST);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM