我有这个奇怪的问题.我的教授似乎无法复制它,他提供的帮助充其量是最小的.所以说到这一点.此代码在面板上生成JButton并添加到JFrame的内容窗格中:
public myPanel extends JPanel {
static final long serialVersionUID = 1;
public myPanel() {
this.setPreferredSize(new Dimension(600, 40));
}
public void paintComponent(Graphics graphicsDrawer) {
super.paintComponent(graphicsDrawer);
this.add(new JButton("A Button"));
}
}
Run Code Online (Sandbox Code Playgroud)
这是GUI代码
public class myGUI {
public myGUI() {
}
public void showGUI() {
JFrame frame = new JFrame("Issues!!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new myPanel());
frame.pack();
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
然后由该类运行GUI
// It is unnecessary to show here but I made a class that
// implements runnable that creates a myGUI object and calls
// …Run Code Online (Sandbox Code Playgroud)