java 如何在 NetBeans GUI 构建器中使用卡片布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1729087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I work with the Card Layout in the NetBeans GUI builder?
提问by Sidharth
Does anyone know how to work with the Card Layout in the NetBeans GUI builder tool? I want to show panels as per the JRadioButtonselection, so I want to lay this out using the Card Layout.
有谁知道如何在 NetBeans GUI 构建器工具中使用卡片布局?我想根据JRadioButton选择显示面板,所以我想使用卡片布局来布置它。
回答by Vincent Ramdhanie
Here is a very simple tutorialthat might start you off in the right direction. I hope it is helpful.
这是一个非常简单的教程,可以让您朝着正确的方向开始。我希望它有帮助。
回答by JRL
The Sun tutorialseems a good place to start to learn about Card Layouts.
Sun教程似乎是开始学习卡片布局的好地方。
Concerning NetBeans per se, simply assign the card layout to the component you wish to assign to (example a JPanel), give it a name, and then for the JPanel child components (other JPanels for example), specify their cardNameproperty. To switch from one to another, you code it in an event.
关于 NetBeans本身,只需将卡片布局分配给您希望分配给的组件(例如 JPanel),为其命名,然后为 JPanel 子组件(例如其他 JPanel)指定它们的cardName属性。要从一个切换到另一个,您可以在事件中对其进行编码。
回答by a question
card.next(yourPanel);will loop through all the components in your mainpanelthen come to first one. To show a component with your own desire try following (think if there are 5 components and you are on the 2 and want to show first then you have to go through rest of all in the Vincent Ramdhanie'sexample, JRL'sanswer is good according to that gives a quick jump to one you want, but here is another way.
card.next(yourPanel);将遍历您的所有组件,mainpanel然后来到第一个。要显示您自己想要的组件,请尝试以下操作(想想如果有 5 个组件并且您在 2 个上并且想首先显示,那么您必须在Vincent Ramdhanie 的示例中完成其余部分,JRL 的答案是好的,因此快速跳转到你想要的,但这是另一种方式。
import javax.swing.JLabel;
import javax.swing.JPanel;
public class myJFrame extends javax.swing.JFrame {
private JPanel panel1, panel2;
/**
* Creates new form myJFrame
*/
public myJFrame() {
initComponents();
panel1=new JPanel();
panel2=new JPanel();
JLabel lb1=new JLabel("This is panel 1");
JLabel lb2=new JLabel("This is panel 2");
panel1.add(lb1);
panel2.add(lb2);
//make more if you want
// contentPanel.add(panel1);//show any of the panel first
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
buttonPanel1 = new javax.swing.JButton();
buttonPanel2 = new javax.swing.JButton();
contentPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
buttonPanel1.setText("Panel 1");
buttonPanel1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonPanel1ActionPerformed(evt);
}
});
buttonPanel2.setText("Panel 2");
buttonPanel2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonPanel2ActionPerformed(evt);
}
});
....
}
private void buttonPanel2ActionPerformed(java.awt.event.ActionEvent evt) {
contentPanel.removeAll();
contentPanel.add(panel2);
contentPanel.repaint();
contentPanel.revalidate();
}
private void buttonPanel1ActionPerformed(java.awt.event.ActionEvent evt) {
contentPanel.removeAll();
contentPanel.add(panel1);
contentPanel.repaint();
contentPanel.revalidate();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new myJFrame().setVisible(true);
}
});
}
private javax.swing.JButton buttonPanel1;
private javax.swing.JButton buttonPanel2;
private javax.swing.JPanel contentPanel;
private javax.swing.JPanel jPanel1;
}
This way is used when you have a tree and show a panel or component on a tree selection. It shows directly that component. On the tree add a value change listenerand get the selection item and show appropriate panel.
当您拥有一棵树并在树选择上显示面板或组件时,将使用这种方式。它直接显示该组件。在树上添加一个value change listener并获取选择项并显示适当的面板。

