gpt4 book ai didi

java - 在 JToggle 按钮的 Jpanel onclick 中添加或删除 JTextPane

转载 作者:行者123 更新时间:2023-12-02 01:58:07 36 4
gpt4 key购买 nike

我有一个 JDialog,其中有一个名为“dialogPane”的 Main Jpanel。

此 JPanel“dialogPane”有以下 2 个 JPanel

  1. 包含 JTextArea 的 ContentPanel
  2. buttonBar,其中包含进度条和一个切换按钮。

现在我的用例是

  1. 单击“开/关”切换按钮时,TextArea 应隐藏(打开)/显示(关闭)。
  2. 单击切换按钮时,JTextArea 应隐藏,并且对话框大小应减小(小)
  3. 再次单击切换按钮时,应显示 JTextArea 并且应调整对话框大小(大)

下面是我的切换按钮的 Action 监听器。我的代码只是删除了 JTextArea,但对话框的大小仍然很大

private void ShowOrHideLogToggleButtonItemStateChanged(final ItemEvent e) {
final int state = e.getStateChange();
if (state == ItemEvent.SELECTED) {
ShowOrHideLogToggleButton.setText("Show Log");
dialogPane.remove(contentPanel);
dialogPane.updateUI();
dialogPane.revalidate();
dialogPane.repaint();

} else {
ShowOrHideLogToggleButton.setText("Hide Log");
dialogPane.add(contentPanel);
dialogPane.updateUI();
dialogPane.revalidate();
dialogPane.repaint();
this.pack();
}
}

注意:ContentPanel 是包含 JTextArea 的 Jpanel,dialogPane 是包含 ContentPanel 的父 JPanel

Below is my JDialog hierarchy

最小、完整且可验证的示例(重现问题的代码)

package CollectionsPractice;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

/**
* @author User #2
*/
public class Main
{
public static void main(String[] args) {
JDialog myDialog=new JDialog();
new UpdateDialog(myDialog);
}
}
class UpdateDialog extends JDialog {


public UpdateDialog(final JDialog owner) {
super(owner);
initComponents();
resultTextPane.setText("Sample Text In Result Text Area");
}

private void ShowOrHideLogToggleButtonActionPerformed(final ActionEvent e) {
final JToggleButton btn = (JToggleButton) e.getSource();
if (btn.isSelected()) {
btn.setText("Hide Myy Log");
contentPanel.setVisible(true);
} else {
btn.setText("Show Myy Log");
contentPanel.setVisible(false);
}
pack();
}

private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
dialogPane = new JPanel();
contentPanel = new JPanel();
scrollPane1 = new JScrollPane();
resultTextPane = new JTextArea();
buttonBar = new JPanel();
progressBar = new JProgressBar();
ShowOrHideLogToggleButton = new JToggleButton();
logoutButton = new JButton();

//======== this ========
setTitle("Updater");
setAlwaysOnTop(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setMinimumSize(new Dimension(1024, 800));
setVisible(true);
setName("this");
final Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());

//======== dialogPane ========
{
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
dialogPane.setMinimumSize(new Dimension(640, 480));
dialogPane.setName("dialogPane");
dialogPane.setLayout(new BorderLayout());

//======== contentPanel ========
{
contentPanel.setVisible(false);
contentPanel.setName("contentPanel");
contentPanel.setLayout(new BorderLayout());

//======== scrollPane1 ========
{
scrollPane1.setName("scrollPane1");

//---- resultTextPane ----
resultTextPane.setEditable(false);
resultTextPane.setName("resultTextPane");
scrollPane1.setViewportView(resultTextPane);
}
contentPanel.add(scrollPane1, BorderLayout.CENTER);
}
dialogPane.add(contentPanel, BorderLayout.CENTER);

//======== buttonBar ========
{
buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
buttonBar.setName("buttonBar");
buttonBar.setLayout(new GridBagLayout());
((GridBagLayout) buttonBar.getLayout()).columnWeights = new double[]{1.0, 0.0, 0.0};

//---- progressBar ----
progressBar.setStringPainted(true);
progressBar.setName("progressBar");
buttonBar.add(progressBar, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));

//---- ShowOrHideLogToggleButton ----
ShowOrHideLogToggleButton.setText("Show Log");
ShowOrHideLogToggleButton.setName("ShowOrHideLogToggleButton");
ShowOrHideLogToggleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
ShowOrHideLogToggleButtonActionPerformed(e);
}
});
buttonBar.add(ShowOrHideLogToggleButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));

//---- logoutButton ----
logoutButton.setText("Logout");
logoutButton.setEnabled(false);
logoutButton.setName("logoutButton");
buttonBar.add(logoutButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
dialogPane.add(buttonBar, BorderLayout.SOUTH);
}
contentPane.add(dialogPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}

// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JPanel dialogPane;
private JPanel contentPanel;
private JScrollPane scrollPane1;
private JTextArea resultTextPane;
private JPanel buttonBar;
private JProgressBar progressBar;
private JToggleButton ShowOrHideLogToggleButton;
private JButton logoutButton;
// JFormDesigner - End of variables declaration //GEN-END:variables
}

最佳答案

您需要做的是重新包装您的窗口。这是我的例子:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;


public class WindowRepack implements Runnable {

private static final String TEXT =
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod "
+ "tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. "
+ "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd "
+ "gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem "
+ "ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod "
+ "tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. "
+ "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd "
+ "gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";

private final JTextArea area = new JTextArea(5, 40);

private final JScrollPane scroller = new JScrollPane(area);

private final JPanel mainPanel = new JPanel(new BorderLayout());

@Override
public void run() {
area.setText(TEXT);
area.setLineWrap(true);
area.setWrapStyleWord(true);
JToggleButton tb = new JToggleButton("Show text");
tb.addActionListener(this::updateUI);
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
buttonsPanel.add(tb);

// provide fix width for the buttons panel to avoid horizontal scaling of window.
Dimension defSize = buttonsPanel.getPreferredSize();
// temporary make the scroll bar visible to compute the correct width
scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
buttonsPanel.setPreferredSize(new Dimension(scroller.getPreferredSize().width, defSize.height));
scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// done

mainPanel.add(buttonsPanel, BorderLayout.NORTH);
JFrame frm = new JFrame("Dynamic frame");
frm.add(mainPanel);
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.pack();
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new WindowRepack());
}

private void updateUI(ActionEvent evt) {
JToggleButton btn = (JToggleButton) evt.getSource();
if (btn.isSelected()) {
btn.setText("Hide text");
mainPanel.add(scroller);
} else {
btn.setText("Show text");
mainPanel.remove(scroller);
}
Window w = SwingUtilities.windowForComponent(mainPanel);
w.pack();
}
}

我使用 JFrame 实现了它,但相同的方法也适用于 JDialog

重要:只有当您使用正确的布局管理器来填充对话框时,这种方法才有效。

关于java - 在 JToggle 按钮的 Jpanel onclick 中添加或删除 JTextPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52037273/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com