gpt4 book ai didi

java - 重新绘制 JPanel

转载 作者:行者123 更新时间:2023-11-30 08:10:42 25 4
gpt4 key购买 nike

我有两个包含内容的框架。第一个有一个 jlabel 和 jbutton,当单击它时,它将打开一个新框架。我需要在第二个框架关闭时通过添加另一个 jlabel 来重新绘制第一个框架或具有标签的面板。

//已编辑

    import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class FirstFrame extends JPanel implements KeyListener{

private static String command[];
private static JButton ok;
private static int count = 1;
private static JTextField text;
private static JLabel labels[];
private static JPanel p ;
private static JFrame frame;

public int getCount(){
return count;
}
public static void createWindow(){
JFrame createFrame = new JFrame();
JPanel panel = new JPanel(new GridLayout(2,1));
text = new JTextField (30);
ok = new JButton ("Add");
ok.requestFocusInWindow();
ok.setFocusable(true);
panel.add(text);
panel.add(ok);
text.setFocusable(true);
text.addKeyListener(new FirstFrame());
createFrame.add(panel);
createFrame.setVisible(true);
createFrame.setSize(600,300);
createFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
createFrame.setLocationRelativeTo(null);

createFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.out.println(command[count]);
if(command[count] != null){

p.add(new JLabel("NEW LABEL"));
p.revalidate();
p.repaint();
count++;
System.out.println(count);
}


}
});
if(count >= command.length)
count = 1;


ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(command[count] == null)
command[count] = text.getText();
else
command[count] = command[count]+", "+text.getText();


text.setText("");



}
});
}
public FirstFrame(){
p = new JPanel();
JButton create = new JButton ("CREATE");
command = new String[2];
labels = new JLabel[2];
addKeyListener(this);
create.setPreferredSize(new Dimension(200,100));
//setLayout(new BorderLayout());
p.add(new JLabel("dsafsaf"));
p.add(create);
add(p);
//JPanel mainPanel = new JPanel();
/*mainPanel.setFocusable(false);
mainPanel.add(create);
*/

create.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
createWindow();
}
});

//add(mainPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
frame = new JFrame();
frame.add(new FirstFrame());
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

@Override
public void keyReleased(KeyEvent e) {

}
@Override
public void keyTyped(KeyEvent e) {


}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER)
if(ok.isDisplayable()){
ok.doClick();
return;}


}
}
}
});
}
}

最佳答案

根据我的第一条评论,您最好使用某种类型的对话框,并且可能像 JOptionPane 这样简单。例如,在下面的代码中,我使用 JOptionPane 保存的 JTextField 中的文本创建一个新的 JLabel,然后将其添加到原始 GUI:

import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class FirstPanel2 extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 300;
private JTextField textField = new JTextField("Hovercraft rules!", 30);
private int count = 0;

public FirstPanel2() {
AddAction addAction = new AddAction();
textField.setAction(addAction);

add(textField);
add(new JButton(addAction));
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

private class AddAction extends AbstractAction {
public AddAction() {
super("Add");
}

@Override
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
final JTextField someField = new JTextField(text, 10);
JPanel panel = new JPanel();
panel.add(someField);
int result = JOptionPane.showConfirmDialog(FirstPanel2.this, panel, "Add Label",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
JLabel label = new JLabel(someField.getText());
FirstPanel2.this.add(label);
FirstPanel2.this.revalidate();
FirstPanel2.this.repaint();
}
}
}

private static void createAndShowGui() {
FirstPanel2 mainPanel = new FirstPanel2();

JFrame frame = new JFrame("My Gui");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
<小时/>

此外,不要将 KeyListener 添加到文本组件,因为这是一件危险且不必要的事情。在这里,您最好添加一个 ActionListener,或者如我上面的代码所示,添加一个 Action,以便在按下 Enter 键时执行操作。

<小时/>

编辑
你问:

Just realized it is because of the KeyListener. Can you explain please the addAction ?

这在功能上类似于向 JTextField 添加 ActionListener,因此当您按 enter 时,将调用 actionPerformed(...) 方法,完全相同就像按下 JButton 并激活其 ActionListener 或 Action 一样。一个 Action 就像一个类固醇的“ActionListener”。它不仅充当 ActionListener,还可以为按钮提供文本、图标和其他属性。

关于java - 重新绘制 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30423035/

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