我已經看到這個問題得到了回答。但不適用于BoxLayout。我已經在Java中創建了一個日程安排應用程序,因為我想每小時都顯示一次,所以我選擇了BoxLayout。當每小時都顯示出來時,一切看起來都很完美。然而,在通過單擊按鈕調用我的方法之前,JTextField只占據整個屏幕(使用按鈕)。我該怎么做才不同呢?
Main class:
public class Main {
public static void main(String[] args) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
GamePanel gamepanel = new GamePanel();
window.add(gamepanel);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true)
}
}
GamePanel Class:
public class GamePanel extends JPanel implements ActionListener {
//Screen Settings
final int originalTileSize = 16;
final int scale = 3;
final int tileSize = originalTileSize * scale;
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenWidth = tileSize * maxScreenCol;
final int screenHeight = tileSize * maxScreenRow;
//Initialize the things that are displayed
JButton knopf;
JTextField field;
Tage days;
String textInhalt;
static JLabel displayIsSchool;
static JLabel stunden;
static JLabel stunden1;
static JLabel stunden2;
static JLabel stunden3;
static JLabel stunden4;
static JLabel stunden5;
static JLabel stunden6;
static JLabel stunden7;
static JLabel stunden8;
//Panel constructor
public GamePanel(){
super();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
//JFrame stats
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.darkGray);
this.setDoubleBuffered(true);
this.setFocusable(true);
//Initialize Text field
field = new JTextField();
field.setFont(new Font("Serif",Font.BOLD,30));
field.setPreferredSize(new Dimension (100, 50));
//Initialize Button
knopf = new JButton("klick");
knopf.addActionListener(this);
knopf.setPreferredSize(new Dimension (100, 35));
//Initialize isSchool
displayIsSchool = new JLabel("");
displayIsSchool.setForeground(Color.white);
displayIsSchool.setFont(new Font("Serif",Font.BOLD,30));
//Initialize stunden
stunden = new JLabel("");
stunden.setForeground(Color.white);
stunden.setFont(new Font("Serif",Font.BOLD,30));
stunden1 = new JLabel("");
stunden1.setForeground(Color.white);
stunden1.setFont(new Font("Serif",Font.BOLD,30));
stunden2 = new JLabel("");
stunden2.setForeground(Color.white);
stunden2.setFont(new Font("Serif",Font.BOLD,30));
stunden3 = new JLabel("");
stunden3.setForeground(Color.white);
stunden3.setFont(new Font("Serif",Font.BOLD,30));
stunden4 = new JLabel("");
stunden4.setForeground(Color.white);
stunden4.setFont(new Font("Serif",Font.BOLD,30));
stunden5 = new JLabel("");
stunden5.setForeground(Color.white);
stunden5.setFont(new Font("Serif",Font.BOLD,30));
stunden6 = new JLabel("");
stunden6.setForeground(Color.white);
stunden6.setFont(new Font("Serif",Font.BOLD,30));
stunden7 = new JLabel("");
stunden7.setForeground(Color.white);
stunden7.setFont(new Font("Serif",Font.BOLD,30));
stunden8 = new JLabel("");
stunden8.setForeground(Color.white);
stunden8.setFont(new Font("Serif",Font.BOLD,30));
//Add everything to JFrame
this.add(field);
this.add(knopf);
this.add(displayIsSchool);
this.add(stunden);
this.add(stunden1);
this.add(stunden2);
this.add(stunden3);
this.add(stunden4);
this.add(stunden5);
this.add(stunden6);
this.add(stunden7);
this.add(stunden8);
}
//Call Method for set Day
public void sayIf() {
//Switch statement for user Input
switch(textInhalt)
{
case "Montag":
Montag.sagName();
break;
case "Dienstag":
Dienstag.sagName();
break;
case "Mittwoch":
Mittwoch.sagName();
break;
case "Donnerstag":
Donnerstag.sagName();
break;
case "Freitag":
Freitag.sagName();
break;
case "Samstag":
Samstag.sagName();
break;
case "Sonntag":
Sonntag.sagName();
break;
}
}
//schedule
static String[] monHours = {"Bio", "Bio", "Mathe", "Mathe", "Chemie", "Chemie", "Frei", "Musik", "Musik"};
static String[] dinHours = {"Deutsch", "Mathe", "Physik", "Physik", "Englisch", "Frei", "Franz?sisch", "Frei", "Frei"};
static String[] mitHours = {"Franz?sisch", "Franz?sisch", "Englisch", "Tutor", "Geschichte", "Geschichte", "Frei", "Sport", "Sport"};
static String[] donHours = {"Englisch", "Englisch", "Deutsch", "Deutsch", "Informatik", "Informatik", "Frei", "Frei", "Frei"};
static String[] freHours = {"PoWi", "PoWi", "Ethik", "Ethik", "Deutsch", "Frei", "Mathe", "Frei", "Frei"};
static String[] samHours = {"", "", "", "", "", "", "", "", ""};
static String[] sonHours = {"", "", "", "", "", "", "", "", ""};
//create Day objects
static Tage Montag = new Tage(true, monHours, "Montag");
static Tage Dienstag = new Tage(true, dinHours, "Dinestag") ;
static Tage Mittwoch = new Tage(true, mitHours, "Mittwoch") ;
static Tage Donnerstag = new Tage(true, donHours, "Donnerstag") ;
static Tage Freitag = new Tage(true, freHours, "Freitag") ;
static Tage Samstag = new Tage(false, samHours, "Samstag") ;
static Tage Sonntag = new Tage(false, sonHours, "Sonntag") ;
//keyListener
public void actionPerformed(ActionEvent e) {
if(e.getSource()==knopf) {
textInhalt = field.getText();
sayIf();
}
}
}
標簽(日)類:
package basicsPack;
public class Tage{
//Variables for constructor
boolean isSchool;
String[] stunden;
String name;
//Tage constructor
public Tage(boolean isSchool, String[] stunden, String name) {
this.isSchool = isSchool;
this.stunden = stunden;
this.name = name;
}
//sagName method
public void sagName() {
//Display hours an rather I have school
if(this.isSchool == true) {
GamePanel.displayIsSchool.setText("Du hast am "+ name + " Schule ");
GamePanel.stunden.setText(this.stunden[0]);
GamePanel.stunden1.setText(this.stunden[1]);
GamePanel.stunden2.setText(this.stunden[2]);
GamePanel.stunden3.setText(this.stunden[3]);
GamePanel.stunden4.setText(this.stunden[4]);
GamePanel.stunden5.setText(this.stunden[5]);
GamePanel.stunden6.setText(this.stunden[6]);
GamePanel.stunden7.setText(this.stunden[7]);
GamePanel.stunden8.setText(this.stunden[8]);
}else {
GamePanel.displayIsSchool.setText("Du hast am " + name + " keine Schule");
GamePanel.stunden.setText("");
GamePanel.stunden1.setText("");
GamePanel.stunden2.setText("");
GamePanel.stunden3.setText("");
GamePanel.stunden4.setText("");
GamePanel.stunden5.setText("");
GamePanel.stunden6.setText("");
GamePanel.stunden7.setText("");
GamePanel.stunden8.setText("");
}
}
}
所以,是的,使用JPanel將JTextField和JButton放在BoxLayout中自己的JPanel中,這很好,然后將JPanel放在使用BorderLayout的JPanel BorderLayout.PAGE_START位置,下面是GUI的其余部分。例如:
displays as
其他無關問題:
For example: