为什么我的真正简单的Java程序没有';还没有做任何事情,有时有效,有时无效';T
我刚开始使用Java,以前只使用过PHP——发现很难理解面向对象的东西。我正在使用EclipseIDE 我正在尝试制作一个程序,告诉你你在另一个星球上的体重——看起来很简单 到目前为止,我所做的就是在Swing中创建一半的接口(这就是它的名称吗?) 有时我运行它,它会像我预期的那样出现,带有标题、文本框等。。。。 其他时候(当完全没有改变时),它只会出现一个空白屏幕 该图像显示了它工作时的外观。当它不工作时,就没有对象了。它在大约20%的时间里工作 我想这可能是因为我的下拉菜单——或者说JComboBox,这让我头疼不已——Eclipse让我在每次提到JComboBox之后都添加了“ 我不知道这是为什么,我可能只是太笨了,对不起,如果这是一个愚蠢的问题,但我如何才能解决这个问题,我的代码出了什么问题为什么我的真正简单的Java程序没有';还没有做任何事情,有时有效,有时无效';T,java,eclipse,swing,jcombobox,Java,Eclipse,Swing,Jcombobox,我刚开始使用Java,以前只使用过PHP——发现很难理解面向对象的东西。我正在使用EclipseIDE 我正在尝试制作一个程序,告诉你你在另一个星球上的体重——看起来很简单 到目前为止,我所做的就是在Swing中创建一半的接口(这就是它的名称吗?) 有时我运行它,它会像我预期的那样出现,带有标题、文本框等。。。。 其他时候(当完全没有改变时),它只会出现一个空白屏幕 该图像显示了它工作时的外观。当它不工作时,就没有对象了。它在大约20%的时间里工作 我想这可能是因为我的下拉菜单——或者说JCo
package calc;
import javax.swing.*;
import java.awt.*;
public class View extends JFrame {
static String titleText = "Calculate your Mass on another Plannet";
public View(){
super(titleText);
setSize(500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
FlowLayout flo = new FlowLayout();
setLayout(flo);
JPanel inputData = new JPanel();
//Labels
JLabel lblTitle = new JLabel (titleText, JLabel.CENTER);
lblTitle.setFont(new Font("Arial", Font.BOLD, 24));
JLabel lblInputMass = new JLabel ("Weight", JLabel.LEFT);
JLabel lblInputUnits = new JLabel("Units");
//Input Boxes and Lists
JTextField txtInputMass = new JTextField(5);
JComboBox<Object> comInputUnits;
String arrUnits[] = {"Kilos", "Stone", "Pounds"};
comInputUnits = new JComboBox<Object>(arrUnits);
comInputUnits.setSelectedIndex(1);
//Buttons
JButton btnCalculate = new JButton("Calculate");
//Append objects
add(lblTitle);
inputData.add(lblInputMass);
inputData.add(txtInputMass);
inputData.add(lblInputUnits);
inputData.add(comInputUnits);
inputData.add(btnCalculate);
add(inputData);
}
/**
* @param args
*/
public static void main(String[] args) {
View sal = new View();
}
}
包计算;
导入javax.swing.*;
导入java.awt.*;
公共类视图扩展了JFrame{
静态字符串titleText=“计算另一个平面网上的质量”;
公众观点(){
超级(标题文本);
设置大小(500400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(真);
FlowLayout flo=新的FlowLayout();
setLayout(flo);
JPanel inputData=新的JPanel();
//标签
JLabel lblTitle=新的JLabel(标题文本,JLabel.CENTER);
lblTitle.setFont(新字体(“Arial”,Font.BOLD,24));
JLabel lblInputMass=新的JLabel(“重量”,JLabel.LEFT);
JLabel lblInputUnits=新的JLabel(“单元”);
//输入框和列表
JTextField txtInputMass=新的JTextField(5);
JComboBox comInputUnits;
字符串单位[]={“千克”、“石头”、“磅”};
comInputUnits=新的JComboBox(arrUnits);
comInputUnits.setSelectedIndex(1);
//钮扣
JButton btnCalculate=新JButton(“计算”);
//附加对象
添加(lblTitle);
inputData.add(lblInputMass);
inputData.add(txtInputMass);
inputData.add(lblInputUnits);
输入数据。添加(comInputUnits);
inputData.add(btnCalculate);
添加(输入数据);
}
/**
*@param args
*/
公共静态void main(字符串[]args){
视图sal=新视图();
}
}
对不起,这是一个很长的问题,如果有任何建议或答案,我将不胜感激,
正如我所说,我对Java了如指掌,我才刚刚开始——谢谢:)你应该
- 仅操纵内部的回转组件李>
- 只有在将所有组件添加到框架后,才调用
李>setVisible(true)
- 用一个n来拼写行星,虽然这不太重要
“Eclipse让您添加”的
称为泛型类型。阅读。您需要在EventDispatchThread(EDT)中处理Swing组件
将对new View()
的调用包装在对SwingUtilities.invokeAndWait()的调用中。请尝试以下操作:
public static void main(String[] args) {
View sal = new View();
sal.setSize(500, 400);
sal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sal.setVisible(true);
}
在构造函数后面注释这些行:
super(titleText);
setSize(500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
希望能有所帮助。请随时调用pack()
。此版本工作可靠(至少在外观上如此)
IMHO,您不应该在构造函数中进行太多调用。但这可能无法解决问题。我看不出您在何处将UI组件添加到任何容器中。谢谢,最后的setVisible(true)修复了它:)我试图在internet上查找事件调度线程的哪一部分,但还不太清楚?谢谢你为什么不简单地跟随我的链接,阅读教程呢。这里已经解释过了。哈哈,我不明白:p事件调度线程不仅仅是程序的一部分,就像类一样吗?(对不起)除了@JBNizet第3点。(Plannet->Planet),也将“质量”更改为“重量”。即使在轨道或自由落体中,物体的质量也保持不变,重量随重力而变化。我尝试了你的代码,修改后的代码一直有效:)-1。必须从EDT访问Swing组件。上面的更改并没有改变任何东西。-1因为没有使用setLocationByPlatform(true)
,哈哈,尽管它仍然有我的+1,多么奇怪:-)@nIcEcOw“不包括电池”,这是一项预算工作。如果我是VIP,我会先改变布局,然后去掉“设置大小”的框架,然后…;)
import javax.swing.*;
import java.awt.*;
public class View extends JFrame {
static String titleText = "Calculate your Weight on another Planet";
public View(){
super(titleText);
// not now!
//setSize(500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setVisible(true);
FlowLayout flo = new FlowLayout();
setLayout(flo);
JPanel inputData = new JPanel();
//Labels
JLabel lblTitle = new JLabel (titleText, JLabel.CENTER);
lblTitle.setFont(new Font("Arial", Font.BOLD, 24));
JLabel lblInputMass = new JLabel ("Weight", JLabel.LEFT);
JLabel lblInputUnits = new JLabel("Units");
//Input Boxes and Lists
JTextField txtInputMass = new JTextField(5);
JComboBox comInputUnits;
String arrUnits[] = {"Kilos", "Stone", "Pounds"};
comInputUnits = new JComboBox(arrUnits);
comInputUnits.setSelectedIndex(1);
//Buttons
JButton btnCalculate = new JButton("Calculate");
//Append objects
add(lblTitle);
inputData.add(lblInputMass);
inputData.add(txtInputMass);
inputData.add(lblInputUnits);
inputData.add(comInputUnits);
inputData.add(btnCalculate);
add(inputData);
// force the container to layout the components. VERY IMPORTANT!
pack();
setSize(500,400);
setVisible(true);
}
public static void main(String[] args) {
// This is what JB Nizet was alluding to..
// 'start (or update) the GUI on the EDT'
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new View();
}
});
}
}