我有一个Jpanel
名为的mainPanel
类和另一个名为MyPanel
extends的类JPanel
。我想添加MyPanel
到mainPanel
. 我做了这段代码,没有得到任何结果。
这是我的主要课程mainPanel
:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Main extends JFrame {
private JPanel contentPane;
private final JPanel mainPanel = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
inItGUI();
}
private void inItGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 662, 417);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
mainPanel.setBounds(25, 11, 611, 341);
contentPane.add(mainPanel);
mainPanel.setLayout(null);
mainPanel.add(new MyPanel());// **Here i added MyPanel**
mainPanel.revalidate();
repaint();
}
}
这是我MyPanel
之前添加的 beeb 课程mainPanel
:
import java.awt.Component;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyPanel extends JPanel {
private final JButton btnNewButton = new JButton("New button");
private final JButton btnNewButton_1 = new JButton("New button");
public MyPanel() {
inItGUI();
}
private void inItGUI() {
setLayout(null);
btnNewButton.setBounds(60, 49, 106, 43);
add(btnNewButton);
btnNewButton_1.setBounds(189, 49, 89, 43);
add(btnNewButton_1);
}
}