所以我目前正在做一个项目,我想知道我正在做的事情是否可行。我的想法是创建 3 个框架,每个数据库表一个。然后我会有一个带有“Customer”的按钮,当点击它时,它将使所有其他框架及其组件不可见,同时保持它自己的框架可见。这是可能的还是有更简单的方法?是否可以为每个框架创建 3 个不同的类并将它们链接起来?我把我的代码留在下面,这样你就可以更好地了解我正在尝试的东西。
public class Main extends JFrame {
public Main(){
// Attach window listener
addWindowListener(new WindowCloser()); // Just in-case it's needed
// Adding first customer panel
JPanel pCustomerL = new JPanel();
pCustomerL.setBounds(0,0,750,750);
pCustomerL.setBackground(Color.BLUE);
// Adding second customer panel
JPanel pCustomerR = new JPanel();
pCustomerR.setBounds(750,0,750,750);
pCustomerR.setBackground(Color.BLACK);
// Adding first product panel
JPanel pProductL = new JPanel();
pProductL.setBounds(0,0,750,750);
pProductL.setBackground(Color.YELLOW);
// Adding second product panel
JPanel pProductR = new JPanel();
pProductR.setBounds(750,0,750,750);
pProductR.setBackground(Color.GREEN);
// Adding first invoice panel
JPanel pInvoiceL = new JPanel();
pInvoiceL.setBounds(0,0,750,750);
pInvoiceL.setBackground(Color.BLUE);
// Adding second invoice panel
JPanel pInvoiceR = new JPanel();
pInvoiceR.setBounds(750,0,750,750);
pInvoiceR.setBackground(Color.BLACK);
// Button Listener
ButtonListener listener = new ButtonListener();
// Adding "Customer" Button
JButton b = new JButton("Invoice");
b.addActionListener(listener);
add(b);
b.setBounds(1300,10,150,35);
// Adding "Product" Button
b = new JButton("Product");
b.addActionListener(listener);
add(b);
b.setBounds(1150,10,150,35);
// Adding "Invoice" Button
b = new JButton("Customer");
b.addActionListener(listener);
add(b);
b.setBounds(1000,10,150,35);
// Frame Settings
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(1500,750);
// Customer Panel
this.add(pCustomerL);
this.add(pCustomerR);
// Product Panel
// this.add(pProductL);
// this.add(pProductR);
// Invoice Panel
this.add(pInvoiceL);
this.add(pInvoiceR);
// Customer Panel Settings
pCustomerL.setVisible(false);
pCustomerR.setVisible(false);
// Product Settings
pProductL.setVisible(true);
pProductR.setVisible(true);
// Invoice settings
pInvoiceL.setVisible(false);
pInvoiceR.setVisible(false);
this.setVisible(true);
}
// Listener for buttons
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String buttonLabel = evt.getActionCommand();
}
}
// Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) { System.exit(0);}
}
public static void main(String[] args) {
new Main();
}
}
答案 0 :(得分:0)
当您必须在 Java 中的同一个 Windows 应用程序中控制多个面板时,最推荐的选项是使用 JTabedPane 组件。在其中,您可以根据需要设置不同的面板并更轻松地隐藏/显示它们。
// In this case the look and feel renders the title for the tab.
tabbedPane.addTab("Tab", myComponent);
// In this case the custom component is responsible for rendering the
// title of the tab.
tabbedPane.addTab(null, myComponent);
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));
如果您有兴趣,请尝试对此进行更多研究。
https://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html