[英]Java Swing - Grouped Component
Just a quick question. 只是一个简单的问题。
I'm doing a GUI which involves jTextfield, jButton, jLabels. 我正在做一个涉及jTextfield,jButton,jLabels的GUI。 Is it possible to group up this 3 component and have it hide. 是否可以将这3个组件组合起来并隐藏起来。 Only once a jButton is click, this 3 component will appear? 仅单击一次jButton,这3个组件就会出现吗?
Similar usage for a button to create a new component, but in this case I wish to have it hide and unhide upon click on jButton. 用于创建新组件的按钮的用法类似,但是在这种情况下,我希望在单击jButton时将其隐藏和取消隐藏。
You can add all the components to a JPanel and change the panel's visibility. 您可以将所有组件添加到JPanel并更改面板的可见性。 When doing this you should consider a layout that doesn't use the space when the components are not visible. 执行此操作时,应考虑在组件不可见时不使用空间的布局。 You could use MigLayout and set hideMode 3. 您可以使用MigLayout并设置hideMode 3。
Here's a code example showing how to show/hide a grouped component, which is implemented as a JPanel
as others have suggested. 这是一个代码示例,显示了如何显示/隐藏分组的组件,正如其他人建议的那样,该组件实现为JPanel
。 The GroupedComponent
retains it's size even when hidden. 即使隐藏, GroupedComponent
仍保留其大小。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class SimpleTest extends JFrame {
GroupedComponent test = new GroupedComponent("one", "two", "three");
public SimpleTest() {
super("GroupedComponent Example");
JPanel content = (JPanel)getContentPane();
content.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
final JButton hideButton = new JButton(test.getButtonText());
hideButton.setPreferredSize(new Dimension(100,hideButton.getPreferredSize().height));
hideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
test.toggle();
hideButton.setText(test.getButtonText());
}
});
content.add(hideButton);
content.add(test);
}
public static void main(String[] argv) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SimpleTest c = new SimpleTest();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.pack();
c.setVisible(true);
}
});
}
class GroupedComponent extends JPanel {
boolean visible = true;
JTextField field;
JButton button;
JLabel label;
GroupedComponent(String fieldText, String buttonText, String labelText) {
super(new GridLayout(1, 3, 4, 4));
field = new JTextField(fieldText);
button = new JButton(buttonText);
label = new JLabel(labelText);
add(field);
add(button);
add(label);
setBorder(new CompoundBorder(new LineBorder(Color.lightGray), new EmptyBorder(4,4,4,4)));
}
void toggle() {
if(visible) {
visible = false;
field.setVisible(false);
button.setVisible(false);
label.setVisible(false);
} else {
visible = true;
field.setVisible(true);
button.setVisible(true);
label.setVisible(true);
}
}
String getButtonText() {
return visible ? "Hide" : "Show";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.