2016-11-13 112 views
0

我正在開發一個關於JAVA Swing的項目。我沒有太多的JAVA經驗,我需要你們的幫助。
我使用的是frame.pack(),因此它根據需要採用最佳尺寸。我的screenTwo可以根據來自ScreenOne的輸入而改變。我用frame.setMaximumSize(new Dimension(1300, 500)),但它似乎並沒有工作。
如果框架變大,它與任務欄重疊,我不希望這樣,所以它應該比總屏幕尺寸稍小。如何實現這一目標?
編輯:
代碼:JAVA設置JFrame的最大尺寸

public class Test { 

    private static JFrame frame ; 
    private JRadioButton[] radioButton; 
    public static void main(String[] args) { 
     Test t = new Test(); 
     t.go(); 
     frame.pack(); 
     frame.setMaximumSize(new Dimension(1300, 600)); 
     frame.setResizable(false); 
     frame.setVisible(true); 
     Dimension si = frame.getContentPane().getSize(); 
     System.out.println(si);   //Output: java.awt.Dimension[width=934,height=751] 
    } 
    public void go() 
    { 
     frame = new JFrame(); 
     JPanel panel1 = new JPanel(); 
     frame.getContentPane().add(panel1); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     panel1.setLayout(new BorderLayout(0, 20)); 
     Font font = new Font("Times New Roman", Font.PLAIN, 20); 

     JPanel panel2 = new JPanel(); 
     panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 1)); 
     panel2.add(new JLabel("       Flight ID")); 
     panel2.add(new JLabel("        Departure")); 
     panel2.add(new JLabel("    Arrival  ")); 
     panel2.add(new JLabel("  Transit Time ")); 
     panel2.add(new JLabel("Total Duration      "));   
     panel1.add(BorderLayout.NORTH, panel2); 

     JPanel panel3 = new JPanel(); 
     panel3.setLayout(new GridLayout(0, 1));   

     GridBagConstraints c = new GridBagConstraints(); 
     int comboSize = 15, i; 
     JPanel panelCombo[] = new JPanel[comboSize]; 
     ButtonGroup group = new ButtonGroup(); 
     radioButton = new JRadioButton[comboSize]; 
     /*** 
     Adding a bunch of components in here 
     ***/ 
     for(i=0; i<comboSize; i++) { 
       panelCombo[i] = new JPanel(); 
       panelCombo[i].setLayout(new GridBagLayout()); 
       c.gridx = 0;    
       c.gridy = 0; 
       c.insets = new Insets(0, 50, 0, 50);    
       panelCombo[i].add(new JLabel("Flight 1 ID"), c); 

       c.gridx++; 
       panelCombo[i].add(new JLabel("Dept Time"), c); 

       c.gridx++; 
       panelCombo[i].add(new JLabel("Arr Time"), c); 
       c.gridx++; 
       c.gridheight = 4; 
       panelCombo[i].add(new JLabel("Total time"), c); 
       c.gridx++; 

       panelCombo[i].add(new JLabel("Duration"), c); 

       c.gridx++;   
       radioButton[i] = new JRadioButton(); 
       panelCombo[i].add(radioButton[i], c); 
       group.add(radioButton[i]); 

       c.gridheight = 1; 
       c.gridx = 0; 
       c.gridy++; 
       panelCombo[i].add(new JLabel("Depar to Arrival"), c); 

       c.gridx++; 
       panelCombo[i].add(new JLabel("Date"), c); 

       c.gridx = 0; 
       c.gridy++; 
       panelCombo[i].add(new JLabel("Flight 2 ID"), c); 

       c.gridx++; 
       panelCombo[i].add(new JLabel("Dept Time"), c); 

       c.gridx++; 
       panelCombo[i].add(new JLabel("Arr Time"), c); 

       c.gridx = 0; 
       c.gridy++; 
       panelCombo[i].add(new JLabel("Dept to Arrival"), c); 

       c.gridx++; 
       panelCombo[i].add(new JLabel("Date"), c); 
       panelCombo[i].setBackground(Color.GREEN); 

       panel3.add(panelCombo[i]); 
       panelCombo[i].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     }    
     JScrollPane scroll = new JScrollPane(panel3); 
     scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 
     scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     panel1.add(BorderLayout.CENTER, scroll); 


    } 
} 
+0

[setMaximumSize not working in java]的可能重複(http://stackoverflow.com/questions/4061010/setmaximumsize-not-working-in-java) –

+0

請顯示您當前正在嘗試的代碼,否則,很難幫助 – ItamarG3

+0

@ItamarGreen代碼已添加。期待任何幫助。 – Arindam

回答

4

首先,什麼所有你發佈的代碼是?你的問題是關於幀的首選和最大尺寸。框架的佈局與問題無關。

我們對您的申請不感興趣。我們只對展示問題的代碼感興趣。

例如:

JPanel panel = new JPanel(); 
panel.setPreferredSize(new Dimension(1400, 600)); 

JFrame frame = new JFrame(); 
frame.add(panel); 
frame.pack(); 
frame.setMaximumSize(new Dimension(1300, 500)); 
frame.setVisible(true); 

現在你把上面的代碼在main()方法,你必須演示你的問題的代碼。這是你在論壇發佈的內容,而不是你應用程序的核心轉儲。

如果幀變大,其與任務欄重疊,我不希望出現這種情況,

關於你的問題,你可以使用下面的代碼,以獲得桌面上的可用空間:

GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
Rectangle bounds = env.getMaximumWindowBounds(); 
System.out.println("Screen Bounds: " + bounds); 

添加上面的代碼包後(),然後添加類似以下內容:

int width = Math.min(frame.getWidth(), bounds.width); 
int height = Math.min(frame.getHeight(), bounds.height); 
frame.setSize(new Dimension(width, height)); 
frame.setVisible(true); 
+0

我可能會提到*一般來說,使用包比調用setSize更好,因爲包離開幀佈局管理器負責幀大小*(http://stackoverflow.com/questions/22982295/what-does-pack-做)。但這在**這種情況下並不相關 – ItamarG3