我正在开发一个小型java项目,最终目标是一个程序,告诉您指定月份的天数。我的问题只是我的一个文本域出现在GUI中!我不知道为什么会这样,并且一直在寻找解决方案大约2个小时。请帮忙!附:我是一个新手程序员,至少在java中,所以这可能是一个明显的答案!
我的代码:
package main;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings({ "unused", "serial" })
public class Test extends JFrame {
JTextField text;
JTextField text2 = new JTextField("Enter Year", 40);
JPanel panel = new JPanel();
JLabel label1;
String Input1;
String Input2;
int month;
int year;
int numDays = 0;
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text = new JTextField("Enter month's #", 40);
text2.addActionListener(new MyActionListener());
panel.setPreferredSize(new Dimension(400,800));
getContentPane().add(panel);
getContentPane().add(text, "Center");
getContentPane().add(text2, "Center");
pack();
}
public static void main(String[] args) {
new Test().setVisible(true);
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Input1 = text.getText();
Input2 = text2.getText();
month = Integer.parseInt(Input1);
year = Integer.parseInt(Input2);
math(month, year);
}
}
public void math(int month, int year){
switch (month) {
case 1: case 3: case 5:
case 7: case 8: case 10:
case 12:
numDays = 31;
break;
case 4: case 6:
case 9: case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) &&
!(year % 100 == 0))
|| (year % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
default:
label1 = new JLabel("Invalid input...");
getContentPane().add(label1, JLabel.BOTTOM);
break;
}
label1 = new JLabel(Integer.toString(numDays));
getContentPane().add(label1, JLabel.BOTTOM);
}
}
我确定它可能只是我看过的东西,我希望它们是一个简单的解决方案!提前谢谢!
答案 0 :(得分:1)
首先看看Laying Out Components Within a Container和How to Use BorderLayout
默认情况下, JFrame
使用BorderLayout
,只管理5个可用位置中的每个位置的单个组件,在这种情况下,只会添加您添加的最后一个组件
根据您要实现的目标,将确定您可以使用的布局管理器。
例如,您可以使用GridBagLayout
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
JTextField text;
JTextField text2 = new JTextField("Enter Year", 40);
JLabel label1;
String Input1;
String Input2;
int month;
int year;
int numDays = 0;
public TestPane() {
setLayout(new GridBagLayout());
text = new JTextField("Enter month's #", 40);
text2.addActionListener(new MyActionListener());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(text, gbc);
add(text2, gbc);
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Input1 = text.getText();
Input2 = text2.getText();
month = Integer.parseInt(Input1);
year = Integer.parseInt(Input2);
math(month, year);
}
}
public void math(int month, int year) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0)
&& !(year % 100 == 0))
|| (year % 400 == 0)) {
numDays = 29;
} else {
numDays = 28;
}
break;
default:
label1 = new JLabel("Invalid input...");
add(label1, gbc);
break;
}
label1 = new JLabel(Integer.toString(numDays));
add(label1, gbc);
}
}
}
有关详细信息,请参阅How to Use GridBagLayout。
通常情况下,您使用了许多布局管理器,将您的UI隔离到可操作的单元中,专注于自己容器中每个部分的个别需求,然后通过使用其他布局管理器将它们组合
答案 1 :(得分:0)
两个文本字段都位于中心位置。 Mybe你想把它们添加到面板中。改变这个:
getContentPane().add(panel);
panel.setLayout(new BorderLayout());
panel.add(text, BorderLayout.CENTER);
panel.add(text2, BorderLayout.SOUTH);