- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于我的应用程序,我有一个 JDialog
,它包含一个带有一些基本 JTextFields
和 JButtons
的 JPanel
。这个想法是用一个按钮来扩展 JDialog
以显示第二个 JPanel
,其中包含一些“高级”设置。
我通过调用 setPreferredSize()
和 pack()
实现了这一点,但这不是很优雅。一个“优雅”的解决方案是将第二个 Panel 以某种方式设置为 null,因此当切换状态处于“缩回”时被 pack() 忽略。
抱歉,我无法为您提供代码(那东西大约有 700 个谎言),但就像我说的,基本上是 JDialog 中的两个 JPanel。
这里有一些图片:
问题:有没有什么技巧可以让这个展开的东西在不必为展开/正常状态设置固定尺寸的情况下进行。
最佳答案
可能有几种方法可以做到这一点,例如,您可以根据需要简单地添加和删除底部面板。如果可以的话,我想避免这种情况,因为它会使布局管理变得更加困难。
另一种解决方案是简单地使组件可见/不可见,但您需要找到一个布局管理器,它实际上会将不可见组件视为不存在 (0x0
) - 是的,我有一起进行测试时与一对夫妇的问题...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Window;
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.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class FlipDialog {
public static void main(String[] args) {
new FlipDialog();
}
public FlipDialog() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JButton flip;
private JPanel bottom;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8, 8, 8, 8);
gbc.gridwidth = GridBagConstraints.REMAINDER;
JPanel top = new JPanel();
flip = new JButton("+");
top.add(flip);
add(top, gbc);
bottom = new JPanel();
bottom.add(new JLabel("Boo"));
bottom.setVisible(false);
add(bottom, gbc);
flip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bottom.setVisible(!bottom.isVisible());
if (bottom.isVisible()) {
flip.setText("-");
} else {
flip.setText("+");
}
revalidate();
Window window = SwingUtilities.windowForComponent(bottom);
window.pack();
}
});
}
}
}
应该注意的是,调用 revalidate
可能是无关紧要的,因为无论如何您都在打包窗口
更新了布局示例
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test100 {
public static void main(String[] args) {
new Test100();
}
public Test100() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SearchPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class SearchPane extends JPanel {
private ObjectsPane objectsPane;
private AdvanceSettingsPane advanceSettingsPane;
public SearchPane() {
setBorder(new EmptyBorder(8, 8, 8, 8));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
objectsPane = new ObjectsPane();
add(objectsPane, gbc);
gbc.gridy++;
gbc.weighty = 0;
advanceSettingsPane = new AdvanceSettingsPane();
advanceSettingsPane.setVisible(false);
add(advanceSettingsPane, gbc);
objectsPane.addExpandCollapseListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println(objectsPane.isExpanded());
advanceSettingsPane.setVisible(objectsPane.isExpanded());
Window window = SwingUtilities.windowForComponent(SearchPane.this);
window.pack();
}
});
}
public class ObjectsPane extends JPanel {
private JSpinner findField;
private JTextField replaceField;
private JButton expandButton;
private JButton replaceButton;
private JButton replaceAllButton;
private boolean expanded = false;
public ObjectsPane() {
setLayout(new GridBagLayout());
findField = new JSpinner(new AbstractSpinnerModel() {
@Override
public Object getValue() {
return "";
}
@Override
public void setValue(Object value) {
}
@Override
public Object getNextValue() {
return "";
}
@Override
public Object getPreviousValue() {
return "";
}
});
replaceField = new JTextField(10);
replaceButton = new JButton("Replace");
replaceAllButton = new JButton("Replace All");
expandButton = new JButton("+");
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Objects found:"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0;
add(new JLabel("Find:"), gbc);
gbc.gridy = 2;
add(new JLabel("Replace:"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(findField, gbc);
gbc.gridy = 2;
add(replaceField, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 1;
gbc.weightx = 0;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.NONE;
add(expandButton, gbc);
JPanel pnlButtons = new JPanel(new GridLayout(1, 2));
pnlButtons.add(replaceButton);
pnlButtons.add(replaceAllButton);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(pnlButtons, gbc);
expandButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
expanded = !expanded;
if (expanded) {
expandButton.setText("-");
} else {
expandButton.setText("+");
}
fireStateChanged();
}
});
}
public boolean isExpanded() {
return expanded;
}
public void addExpandCollapseListener(ChangeListener listener) {
listenerList.add(ChangeListener.class, listener);
}
public void removeExpandCollapseListener(ChangeListener listener) {
listenerList.remove(ChangeListener.class, listener);
}
protected void fireStateChanged() {
ChangeListener[] listeners = listenerList.getListeners(ChangeListener.class);
if (listeners.length > 0) {
ChangeEvent evt = new ChangeEvent(this);
for (ChangeListener listener : listeners) {
listener.stateChanged(evt);
}
}
}
}
public class AdvanceSettingsPane extends JPanel {
public AdvanceSettingsPane() {
setBorder(new TitledBorder("Advance Settings"));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
// gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(new JCheckBox("Live Update"), gbc);
add(new JCheckBox("Word search"), gbc);
add(new JCheckBox("Ignore Case"), gbc);
}
}
}
}
关于java可扩展的JDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26876827/
如何在另一个 JDialog 中添加 JDialog? 最佳答案 JDialog secondDialog = new JDialog(this); // ("this" is the first J
我有一个 JDialog,其中有一个可打开新窗口的按钮。我想要做的是每当其他窗口打开时阻止此 JDialog。当我说阻止时,我的意思是用户无法操纵它,无法移动它或最大化或任何东西。 顺便问一下,对于带
我有一个从 JFrame 上的按钮打开的父 JDialog。父 JDialog 本身有一个从父对话框上的按钮打开的子 JDialog。当我关闭父对话框并使用框架上的按钮再次打开它时,我不希望子对话框也
抱歉这个奇怪的标题,但这是解释。所以我有一个带有学生列表的 StudentRepository 类,这些学生是在 GUI 上选择的(通过 TableModel)。学生对象的属性是: int stude
我有一个未修饰的模态 JDialog,当用户在模态对话框外单击时,我想将其设置为 setVisible(false)。 这在 Swing 中可能吗? 我正在做的是为日期选择器之类的文本字段弹出一个自定
我有一个模式设置对话框,它是一个 JDialog。在这个设置窗口中,我放置了一些组件,包括一个按钮到另一个模态设置对话框,它也是一个 JDialog。我制作了它们 JDialogs,因为这是我所知道的
我只想在单击 JDialog 之外时关闭 JDialog import javax.swing.JDialog; import javax.swing.JLabel; public class Dia
我正在尝试重现我在多个应用程序中看到的功能:我有一个带有多个 JDialog 的 GUI 应用程序。我想轻松地将它们紧密地组织在屏幕上:当我移动一个 JDialog,并且它的一个边界变得“接近”(例如
对 Java 相当陌生,并且遇到了 z 顺序问题。我有一个旧版 Java 应用程序,它有一个主窗口 A,它会弹出一个模式 JDialog B。单击 B 上的按钮后,会弹出一个模式对话框 C。 对于从
我的应用程序中有多个 JDialogs 存储在 map 中。这些JDialogs都有 setModel(false); 当这些对话框失去焦点并且我想将特定的 JDialog 带到前面时,所有 JDia
我有一个扩展 JDialog 的类。当 JDialog 显示时,我单击其启动 Jframe 的显示按钮,但在关闭 JDialog 之前我无法访问 JFrame。当屏幕上存在 JDialog 时,如何访
场景是这样的我的 JFrame 有一个按钮,单击它会打开一个 JDialog,它是一个模型对话框。JDialog 有另一个按钮,我想打开另一个 JFrmae 点击它打开。 结果:另一个 Jframe
我有一个带有主 JFrame 的小型应用程序,它以模态方式打开 JDialog。在这个 JDialog 中,我启动了一个 javax.swing.Timer,它应该在 JDialog 关闭时停止。 p
如何将用户凭据传回到包含的 JFrame,以便 JFrame 知道特定用户? JFrame 有一个 main 方法。 包含的 JFrame 能否以某种方式从 Dialog 中获取用户? 当jbtOk
我基本上创建的是一个 JDialog,它在表单上有一个关键事件。因此,当例如按下空间时,它会做一些事情。在我在同一个对话框上创建一个可编辑的 JTextArea 之前,这种方法工作得很好。当我这样做时
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在构建我的第一个 gui,到目前为止一切正常,除了 JDialog 的故障。 .它在第一次使用时相应地接受名称和进程列表。但是当我把它拉回来输入新的输入时,它仍然没有响应。我认为这不是线程问题,因
如何创建一个 Modal JDialog,在任务正在处理时显示“正在加载”,并在超过 3 秒后显示? 最佳答案 为了扩展 Paul 的回答,SwingWorker 可以很好地运行您的后台任务。然后您可
该程序大部分运行正常,但没有打开任何窗口。它应该在桌面右下角显示一个小对话框。但是对于另一个人来说,编译相同的代码没有问题。我们有相同的 Java 运行时 (1.8_u40)。我该如何解决这个问题?
我正在使用 Eclipse 的 Window Builder 插件。 当我执行以下代码时,它正确显示JDialog。我原本希望 JDialog 也能显示在设计选项卡中(在设计时),但它不会。 pack
我是一名优秀的程序员,十分优秀!