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)
首先,您应该为窗口创建一些 ContentPane(我猜您的意思是 JFrame)。将按钮直接添加到窗口不是一个好主意:P 接下来,您可以将按钮添加到该窗格:
panel.addComponent(button);
最后要做的是:
frame.setContentPane(panel)
这就是全部:P 简而言之;)
使用这样的东西
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);
}
}
saveButton.setVisible(true);
your_window.add(saveButton);
就这样。