我正在编写一个将使用大量面板的程序。我想将每个面板放在他们自己的文件中,以便更容易组织。
我正在使用cardlayout
我的问题是,在其他类文件中,我无法更改另一个面板的可见性,因为该框架位于主类中,而面板将位于另一个类中。
我写了一些封装问题的示例代码。第一个面板位于主类中,它可以切换到第二个外部面板,但外部面板无法切换到主类面板。
这是我的主要课程:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Arrow{
public JPanel panelHouse;
public JFrame frame;
public int total = 3000;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Arrow window = new Arrow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Arrow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(400, 400, 909, 572);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
JPanel panelHouse = new JPanel();
panelHouse.setBounds(0, 0, 10, 10);
frame.getContentPane().add(panelHouse, "Housing");
panelHouse.setLayout(null);
Flash Flash1 = new Flash();
frame.getContentPane().add(Flash1, "Flash");
Flash1.setLayout(null);
JLabel lblHouseRent = new JLabel("RENT");
lblHouseRent.setFont(new Font("Times New Roman", Font.PLAIN, 20));
lblHouseRent.setBounds(99, 53, 73, 35);
panelHouse.add(lblHouseRent);
JLabel lblHouseOwn = new JLabel("OWN");
lblHouseOwn.setFont(new Font("Times New Roman", Font.PLAIN, 20));
lblHouseOwn.setBounds(476, 66, 60, 22);
panelHouse.add(lblHouseOwn);
JButton btnHouseNext = new JButton("NEXT STATION");
btnHouseNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Flash1.setVisible(true);
panelHouse.setVisible(false);
}
});//Close housingNext button actionlistener
btnHouseNext.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnHouseNext.setBounds(344, 371, 184, 48);
panelHouse.add(btnHouseNext);
JLabel lblHousing = new JLabel("HOUSING");
lblHousing.setFont(new Font("Times New Roman", Font.PLAIN, 22));
lblHousing.setBounds(388, 11, 102, 27);
panelHouse.add(lblHousing);
}//close initialize
public JPanel getPanelHouse() {
return panelHouse;
}
}//close class body
这是第二个外部课程。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Flash extends JPanel{
private static final long serialVersionUID = 1L;
public Flash() {
Arrow a = null;
JPanel Flash1 = new JPanel();
setBounds(0, 0, 10, 10);
setLayout(null);
setBackground(Color.orange);
JButton buttonFlash = new JButton("NEXT STATION");
buttonFlash.addActionListener(new ActionListener() {
@SuppressWarnings("null")
public void actionPerformed(ActionEvent arg0) {
Flash1.setVisible(false);
a.getPanelHouse().setVisible(true);
}
});
buttonFlash.setFont(new Font("Times New Roman", Font.PLAIN, 20));
buttonFlash.setBounds(344, 371, 184, 48);
add(buttonFlash);
setVisible(true);
}
}
我需要在Flash类中初始化Arrow,但它会向我发送一个null void警告,因为从技术上来说没有任何东西。
任何想法或帮助都会很棒!