我真的需要帮助。我有一个JFrame与一些JTextFields,我已禁用。我希望通过单击一下JToggleButton(“编辑”)来启用它们,这也是在JFrame上。因此,在costructor中,我将一个 boolean isEditable参数(它是一个字段)传递给所有JTextField的.isEditable()方法,以便在JFrame显示并单击editButton之后发生的是isEditable值更改为 true ,并重新绘制整个JFrame。问题是JFrame拒绝重新绘制!我能做什么?我会非常感激我得到的任何帮助。谢谢:)
代码:
package job;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class TestFrame extends JFrame implements ItemListener{
private JPanel contentPane;
private boolean saveEnabled = false;
private boolean editEnabled = false;
private String actionCommand;
private Node n;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
setBounds(50, 60, 600, 640);
JLabel detailsBanner = new JLabel();
contentPane.add(detailsBanner, BorderLayout.NORTH);
JPanel contentDetail = new JPanel();
contentDetail.setLayout(new BoxLayout(contentDetail, BoxLayout.PAGE_AXIS));
JPanel namePanel = new JPanel();
namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.LINE_AXIS));
namePanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 10));
String nameString = "Sample";
setTitle(nameString);
JLabel nameText = new JLabel(nameString);
namePanel.add(nameText);
contentDetail.add(namePanel);
JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
contentDetail.add(sep);
JPanel commanderPanel = new JPanel();
commanderPanel.setLayout(new BoxLayout(commanderPanel, BoxLayout.PAGE_AXIS));
JPanel commanderNamePanel = new JPanel();
JLabel commanderTitle = new JLabel("Commander");
JTextField commanderName = new JTextField("Sample");
commanderName.setEnabled(editEnabled);
commanderNamePanel.setLayout(new GridLayout(1,2));
commanderNamePanel.add(commanderTitle);
commanderNamePanel.add(commanderName);
commanderPanel.add(commanderNamePanel);
contentPane.add(commanderPanel);
JPanel mainAuxButtonPanel = new JPanel();
mainAuxButtonPanel.setLayout(new BorderLayout(0, 0));
JPanel auxButtonPanel = new JPanel();
GridBagLayout auxButtonsLayout = new GridBagLayout();
JPanel sepPanel = new JPanel();
GridBagLayout sepLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(30, 0, 0, 0);
c.fill = GridBagConstraints.HORIZONTAL;
sepPanel.setLayout(sepLayout);
sepPanel.add(new JSeparator(SwingConstants.HORIZONTAL), c);
mainAuxButtonPanel.add(sepPanel, BorderLayout.NORTH);
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(10, 5, 0, 0);
auxButtonPanel.setLayout(auxButtonsLayout);
JButton back = new JButton("Back");
auxButtonsLayout.setConstraints(back, c);
back.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
auxButtonsLayout.setConstraints(back, c);
auxButtonPanel.add(back);
JToggleButton edit = new JToggleButton("Edit");
auxButtonsLayout.setConstraints(edit, c);
edit.addItemListener(this);
auxButtonPanel.add(edit);
JButton save = new JButton("Save");
save.setEnabled(saveEnabled);
auxButtonsLayout.setConstraints(save, c);
back.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
}
});
auxButtonPanel.add(save);
mainAuxButtonPanel.add(auxButtonPanel, BorderLayout.EAST);
contentPane.add(mainAuxButtonPanel, BorderLayout.SOUTH);
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED){
editEnabled = true;
System.out.println(editEnabled);
}else{
editEnabled = false;
System.out.println(editEnabled);
}
System.out.println("repaintin...");
contentPane.repaint();
}
}
答案 0 :(得分:0)
我不知道为什么你需要重新绘制JFrame。
假设您有一个名为field的JTextField,只需添加一个actionlistener并添加如下代码:
filed.setEnabled(!filed.isEnabled());
更重要的是,也许你应该在正确的组件上调用repaint方法,而不是在JFrame上调用。
或尝试revalidate()和类似的方法。
<强>更新
我的意思是,您不应该在重绘方法中更新组件的启用状态。
也许没有必要使用editEnabled变量来保存状态,请尝试这种方式并修改它以满足您的需求:
JToggleButton edit = new JToggleButton("Edit");
auxButtonsLayout.setConstraints(edit, c);
edit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
commanderName.setEnabled(!commanderName.isEnabled());
}
});
auxButtonPanel.add(edit);
如果您有许多JTextField,只需迭代它。