我拥有一个JFrame,并希望使用多个JPanel在它们之间进行交叉。我怎样才能做到这一点?我试过这样但是没有。
的JFrame
package gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
@SuppressWarnings("serial")
public class Form_Main extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Form_Main frame = new Form_Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Form_Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JPanel pg = new Panel_Giris();
panelEkle(pg);
}
public void panelEkle(JPanel j) {
j.setBorder(new EmptyBorder(5, 5, 5, 5));
j.setLayout(null);
setContentPane(j);
}
}
的JPanel
package gui;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class Panel_Giris extends JPanel {
private JTextField textField;
private JTextField textField_1;
/**
* Create the panel.
*/
public Panel_Giris() {
setLayout(null);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(68, 95, 70, 15);
add(lblNewLabel);
textField = new JTextField();
textField.setBounds(177, 93, 114, 19);
add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("New label");
lblNewLabel_1.setBounds(68, 143, 70, 15);
add(lblNewLabel_1);
textField_1 = new JTextField();
textField_1.setBounds(177, 141, 114, 19);
add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Form_Main fm = new Form_Main();
JPanel ana = new Panel_A();
fm.panelEkle(ana);
}
});
btnNewButton.setBounds(177, 193, 117, 25);
add(btnNewButton);
}
}
我有一个名为panelEkle()的空格来添加一个面板,在这个过程中我从外面取出面板对象并将其打印在框架上,但它不起作用,因为我错过了一些东西。
我的逻辑错了吗?
答案 0 :(得分:0)
要在Frame上添加单独的JPanel,您可以将单独的面板添加到主框架中的面板,如下所示:
public void addPanelToMainFrame(JPanel backPanel, JPanel frontPanel) {
backPanel.removeAll();
backPanel.revalidate();
backPanel.setLayout(new BorderLayout());
backPanel.add(frontPanel);
backPanel.repaint();
}