我遇到了一个非常奇怪的问题。我有一个包含JTextFields和JComboBoxes的JPanel。我可以在JPanel加载时更改JComboBox,但是当我触摸或编辑其中一个JTextField时,它不会让我更改任何组合框...
这是一个有效的MCVE。您可以立即使用它,并且您将看到错误。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Main extends JFrame{
private static JTextField s;
private static JTabbedPane tabbedPane;
private static JPanel config;
private static JComboBox<Object> dns;
private static JComboBox<Object> dnsmm;
private static JButton save;
private static String interval;
private static String dnsen;
private static String dnsm;
private static JPanel server;
static JPanel contentPane;
public static void main(String[] args) throws InterruptedException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() throws IOException {
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 655, 470);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(1, 1));
setContentPane(contentPane);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, BorderLayout.CENTER);
setResizable(false);
server = new JPanel();
server.setLayout(new BorderLayout(0, 0));
ServerPanel();
}
public static void ServerPanel() throws IOException{
tabbedPane.addTab("Manage Server", server);
InputStream in = new FileInputStream("config.dedserver");
Properties p = new Properties();
p.load(in);
in.close();
String saveInt = p.getProperty("saveInterval");
String dnse = p.getProperty("enableDns");
String dnsmo = p.getProperty("serverMode");
config = new JPanel();
server.add(config, BorderLayout.CENTER);
config.setLayout(new BoxLayout(config, BoxLayout.PAGE_AXIS));
//saveint
Panel panel_6 = new Panel();
config.add(panel_6);
JLabel sl = new JLabel("Save Interval (milliseconds): ");
panel_6.add(sl);
s = new JTextField(saveInt);
panel_6.add(s);
s.setColumns(10);
//dnsenabled
Panel panel_9 = new Panel();
config.add(panel_9);
JLabel dnsl = new JLabel("DNS Enabled: ");
panel_9.add(dnsl);
String[] dnsS = { "true", "false" };
dns = new JComboBox<Object>(dnsS);
dns.setSelectedItem(dnse);
dns.addActionListener(dns);
panel_9.add(dns);
//dnsmode
Panel panel_10 = new Panel();
config.add(panel_10);
JLabel dnsml = new JLabel("DNS Server Mode: ");
panel_10.add(dnsml);
String[] dnsm = { "local", "remote" };
dnsmm = new JComboBox<Object>(dnsm);
dnsmm.setSelectedItem(dnsmo);
dnsmm.addActionListener(dnsmm);
panel_10.add(dnsmm);
JPanel panel_7= new JPanel();
config.add(panel_7);
save = new JButton("Save");
panel_7.add(save);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
errorCheck();
}
});
}
public static void errorCheck(){
interval = s.getText();
dnsen = (String) dns.getSelectedItem();
dnsm = (String) dnsmm.getSelectedItem();
interval = checkValues(interval, "60000", "save interval");
saveValues();
}
public static String checkValues(String value, String def, String name){
String val;
try{
Long.parseLong(value);
val = ""+value+"";
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "You did not enter a valid number for the "+ name + " field! It has been set to the default.");
val = def;
}
return val;
}
public static void saveValues(){
try {
Properties props = new Properties();
props.setProperty("saveInterval", interval);
props.setProperty("serverMode", dnsm);
props.setProperty("enableDns", dnsen);
File f = new File("config.dedserver");
OutputStream out = new FileOutputStream(f);
props.store(out, "");
JOptionPane.showMessageDialog(null, "Saved Config Values!");
}
catch (Exception e ) {
e.printStackTrace();
}
}
}
要使它工作,您还需要在项目的根目录中创建一个config.dedserver并将以下内容添加到其中:
#
#Thu Jul 09 08:29:33 BST 2015
saveMethod=h2
startingGems=9999999999
enableDns=true
startingGold=9999999999
startingDarkElixir=9999999999
startingElixir=9999999999
serverMode=remote
saveInterval=30000
maxNameChanges=100
对于整个ServerPanel.java代码,它位于:http://pastebin.com/tvBENHQa
我不确定为什么这不起作用。有没有人有任何想法?
谢谢!
答案 0 :(得分:1)
将Panel
替换为JPanel
,您正在混合awt Component with Swing导致问题。
Swing组件是“轻量级”(由Java呈现)而AWT 组件是“重量级”(作为主机中的组件实现) 平台) - 这意味着如果你放置AWT你会遇到问题 Swing组件内的组件(反之亦然)
//saveint
// Panel panel_6 = new Panel();// replace as below
JPanel panel_6 = new JPanel();
//dnsenabled
// Panel panel_9 = new Panel();
JPanel panel_9 = new JPanel();
//dnsmode
//Panel panel_10 = new Panel();
JPanel panel_10 = new JPanel();
并删除@KDM所说的动作监听器
答案 1 :(得分:0)
虽然JComboBox
实现ActionListener
接口,但它是供内部使用的。你不应该将自己添加到addActionListener
。删除它,看看它是否有帮助。
这是更新的代码。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Main extends JFrame{
private static JTextField s;
private static JTabbedPane tabbedPane;
private static JPanel config;
private static JComboBox<Object> dns;
private static JComboBox<Object> dnsmm;
private static JButton save;
private static String interval;
private static String dnsen;
private static String dnsm;
private static JPanel server;
static JPanel contentPane;
public static void main(String[] args) throws InterruptedException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() throws IOException {
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 655, 470);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(1, 1));
setContentPane(contentPane);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, BorderLayout.CENTER);
setResizable(false);
server = new JPanel();
server.setLayout(new BorderLayout(0, 0));
ServerPanel();
}
public static void ServerPanel() throws IOException{
tabbedPane.addTab("Manage Server", server);
InputStream in = new FileInputStream("config.dedserver");
Properties p = new Properties();
p.load(in);
in.close();
String saveInt = p.getProperty("saveInterval");
String dnse = p.getProperty("enableDns");
String dnsmo = p.getProperty("serverMode");
config = new JPanel();
server.add(config, BorderLayout.CENTER);
config.setLayout(new BoxLayout(config, BoxLayout.PAGE_AXIS));
//saveint
Panel panel_6 = new Panel();
config.add(panel_6);
JLabel sl = new JLabel("Save Interval (milliseconds): ");
panel_6.add(sl);
s = new JTextField(saveInt);
panel_6.add(s);
s.setColumns(10);
//dnsenabled
Panel panel_9 = new Panel();
config.add(panel_9);
JLabel dnsl = new JLabel("DNS Enabled: ");
panel_9.add(dnsl);
String[] dnsS = { "true", "false" };
dns = new JComboBox<Object>(dnsS);
dns.setSelectedItem(dnse);
panel_9.add(dns);
//dnsmode
Panel panel_10 = new Panel();
config.add(panel_10);
JLabel dnsml = new JLabel("DNS Server Mode: ");
panel_10.add(dnsml);
String[] dnsm = { "local", "remote" };
dnsmm = new JComboBox<Object>(dnsm);
dnsmm.setSelectedItem(dnsmo);
panel_10.add(dnsmm);
JPanel panel_7= new JPanel();
config.add(panel_7);
save = new JButton("Save");
panel_7.add(save);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
errorCheck();
}
});
}
public static void errorCheck(){
interval = s.getText();
dnsen = (String) dns.getSelectedItem();
dnsm = (String) dnsmm.getSelectedItem();
interval = checkValues(interval, "60000", "save interval");
saveValues();
}
public static String checkValues(String value, String def, String name){
String val;
try{
Long.parseLong(value);
val = ""+value+"";
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "You did not enter a valid number for the "+ name + " field! It has been set to the default.");
val = def;
}
return val;
}
public static void saveValues(){
try {
Properties props = new Properties();
props.setProperty("saveInterval", interval);
props.setProperty("serverMode", dnsm);
props.setProperty("enableDns", dnsen);
File f = new File("config.dedserver");
OutputStream out = new FileOutputStream(f);
props.store(out, "");
JOptionPane.showMessageDialog(null, "Saved Config Values!");
}
catch (Exception e ) {
e.printStackTrace();
}
}
}