使用BoyLayout管理器設置JTextField的大小。Java

我已經看到這個問題得到了回答。但不適用于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的其余部分。例如:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Main2b {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GamePanel2 mainPanel = new GamePanel2();

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

}

@SuppressWarnings("serial")
class GamePanel2 extends JPanel {
    private JTextField field = new JTextField(10);
    private JButton knopf = new JButton("klick");
    
    public GamePanel2() {
        knopf.setMnemonic(KeyEvent.VK_K);
        
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS));
        topPanel.add(field);
        topPanel.add(knopf);
        
        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.PAGE_START);
        add(Box.createRigidArea(new Dimension(650, 400)));
        
    }
}

displays as


其他無關問題:

  • 避免使用靜態變量,除非特別需要使用它們(這里不是這樣),例如類的常量或必須屬于類而不是實例的字段。
  • 使用arrays或ArrayLists減少代碼重復。這將使您的代碼更容易調試和增強。
  • 此外,您可以為JTextField提供一個ActionListener,與按鈕相同,當按下enter鍵且文本字段有焦點時,它將被觸發。
  • 如果用戶應該只輸入一周中的幾天,而不輸入其他內容,那么不要使用JTextField,因為用戶可以在其中輸入任何內容。相反,使用JComboBox,他們只能選擇您提供給他們的字符串。

For example:

@SuppressWarnings("serial")
class GamePanel2 extends JPanel {
    public static final String[] TAG_DER_WOCH = { "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag",
            "Sonntag" };
    private JComboBox<String> tagDerWochCombo = new JComboBox<>(TAG_DER_WOCH);
    // private JTextField field = new JTextField(10);
    // private JButton knopf = new JButton("klick");

    public GamePanel2() {
        tagDerWochCombo.setSelectedIndex(-1);
        // knopf.setMnemonic(KeyEvent.VK_K);
        
        tagDerWochCombo.addActionListener(e -> {
            System.out.println(tagDerWochCombo.getSelectedItem());
        });

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS));
        topPanel.add(tagDerWochCombo);
        // topPanel.add(knopf);

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.PAGE_START);
        add(Box.createRigidArea(new Dimension(650, 400)));

    }
}
主站蜘蛛池模板: 亚洲一区二区在线视频| 一区二区三区国产| 波多野结衣中文一区| 国产一区二区三区在线电影| 日韩福利视频一区| 国产一区二区在线观看麻豆| 韩国一区二区三区| 一区二区三区免费视频播放器| 久久精品无码一区二区三区日韩| 色狠狠色噜噜Av天堂一区| 麻豆亚洲av熟女国产一区二| 亚洲国产欧美国产综合一区 | 一区免费在线观看| 精品一区二区在线观看| 国产一区在线mmai| 狠狠做深爱婷婷综合一区 | 亚洲一区电影在线观看| 精品一区二区三区在线视频观看| 国产免费播放一区二区| 日韩一区二区超清视频| 国产成人无码一区二区在线观看 | 日韩视频一区二区| 无码一区二区三区在线观看| 国产一区二区三区影院| 中文字幕精品无码一区二区三区| 精品视频一区二区三三区四区| 极品少妇伦理一区二区| 色噜噜狠狠一区二区| 日韩人妻无码一区二区三区久久99 | 99精品国产高清一区二区三区| 国产成人精品一区二三区熟女 | 亚欧在线精品免费观看一区| 国产综合无码一区二区三区| 爱爱帝国亚洲一区二区三区| 老熟妇仑乱一区二区视頻| 免费视频精品一区二区三区| 一区二区高清在线观看| 在线视频一区二区日韩国产| 欧美日韩综合一区二区三区| 久久一区二区明星换脸| 99精品国产一区二区三区|