2015-03-08 176 views
1

當我編譯並運行我的程序時,我希望能夠重新調整它的大小並保持具有相同比例因子的組件。這意味着當框架展開時,組件也會展開,從而保持原始尺寸和間距尺度。Swing組件的自動調整大小

import javax.swing.*; 
import java.awt.*; 
import java.io.*; 
import java.lang.*; 

public class ResizeTst 
{ 
JFrame myMainWindow = new JFrame("This is my title"); 

JPanel firstPanel = new JPanel(); 

//Components 
JButton CompAth = new JButton(); 
JButton ViewAth = new JButton(); 
JButton UpdateRD = new JButton(); 
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
String []fontFamilies = ge.getAvailableFontFamilyNames(); 
Font FontT5 = new Font("SansSerif", Font.BOLD, 50); 
/////////// 

public void runGUI() 
{ 
    myMainWindow.setBounds(10, 10, 1296, 756); 
    myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    myMainWindow.setLayout(new GridLayout(1,1)); 

    createFirstPanel(); 

    myMainWindow.getContentPane().add(firstPanel); 

    myMainWindow.setVisible(true); 
} 

public void createFirstPanel() 
{ 
    firstPanel.setLayout(null); 

    CompAth.setLocation(500,250); 
    CompAth.setSize(320,300); 
    CompAth.setText("<html><CENTER>Compare<br>Athletes</CENTER></html>"); 
    CompAth.setFont(FontT5); 
    firstPanel.add(CompAth); 

    ViewAth.setLocation(100,250); 
    ViewAth.setSize(320,300); 
    ViewAth.setText("<html><CENTER>View<br>Athletes</CENTER></html>"); 
    ViewAth.setFont(FontT5); 
    firstPanel.add(ViewAth); 

    UpdateRD.setLocation(900,250); 
    UpdateRD.setSize(320,300); 
    UpdateRD.setText("<html><CENTER>Update<br>Running<br>Details</CENTER></html>"); 
    UpdateRD.setFont(FontT5); 
    firstPanel.add(UpdateRD); 
} 

public static void main(String[] args) 
{ 
    ResizeTst rt = new ResizeTst(); 
    rt.runGUI(); 
} 
} 

所以,如果我實現了這個代碼,它可以讓我在自動全尺寸的任何系統上運行,而不比例因子改變了原先的原先的組件的大小。允許我的程序在全屏幕的大屏幕或小屏幕上看起來很糟糕時運行。

import javax.swing.*; 
import java.awt.*; 
import java.io.*; 
import java.lang.*; 

public class ResizeTst 
{ 
JFrame myMainWindow = new JFrame("This is my title"); 

JPanel firstPanel = new JPanel(); 

//Components 
JButton CompAth = new JButton(); 
JButton ViewAth = new JButton(); 
JButton UpdateRD = new JButton(); 
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
String []fontFamilies = ge.getAvailableFontFamilyNames(); 
Font FontT5 = new Font("SansSerif", Font.BOLD, 50); 
/////////// 
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 

public void runGUI() 
{ 
    myMainWindow.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    myMainWindow.setSize(screenSize); 
    myMainWindow.setVisible(true);  
    myMainWindow.setResizable(true); 
    myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    myMainWindow.setLayout(new GridLayout(1,1)); 

    createFirstPanel(); 

    myMainWindow.getContentPane().add(firstPanel); 

    myMainWindow.setVisible(true); 
} 

有人告訴我,我應該這樣可以讓我做這件事的面板上使用的佈局,但我不知道它的佈局使用以及如何將其正確運用這一計劃的。任何解決方案或建議如何做到這一點將不勝感激。

+1

1)請學習常見的Java命名(命名約定 - 例如'EachWordUpperCaseClass','firstWordLowerCaseMethod()','firstWordLowerCaseAttribute'除非它是一個'UPPER_CASE_CONSTANT')和一致地使用它。 2)以最小尺寸提供ASCII藝術或簡單的GUI佈局圖,並且如果可調整大小,則具有更大的寬度和高度。 3)我們應該關注哪兩個同名的類? 4)'new Font(「SansSerif」,Font.BOLD,50);'用於編譯時檢查,使用'new Font(Font.SANS_SERIF,Font.BOLD,50);' – 2015-03-08 18:00:11

+0

1)我將在Java中使用它將來的命名。 2)我有,但它不會讓我添加圖像,直到我有10個聲望。 3)它們是相同的類,但第二個具有新的全局變量,並且編輯了runGUI()方法以顯示代碼,該代碼是用於自動進入全屏的新代碼。 4)好的 – Dan 2015-03-08 18:08:49

+0

1)我平時對此的回覆是*「不用擔心,我可能**將來會看到這個問題**」*現在做的時間是***!*** 2)您不能嵌入圖像,但將其上傳到圖像共享網站並鏈接到它們。您始終可以創建ASCII藝術。 3)我不明白爲什麼包括在內,我用了第一堂課。 – 2015-03-08 18:15:21

回答

1

從這些行開始。它使用一個GridLayout,這將擴展組件的大小。使用setMargin(Insets)使按鈕變大一些。屏幕截圖縮小了字體大小,調整了所有需要的數字。

enter image description here

import javax.swing.*; 
import java.awt.*; 
import javax.swing.border.EmptyBorder; 

public class ResizeTst { 

    JFrame myMainWindow = new JFrame("This is my title"); 
    JPanel firstPanel = new JPanel(); 

//Components 
    JButton compAth = new JButton(); 
    JButton viewAth = new JButton(); 
    JButton updateRD = new JButton(); 
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    String[] fontFamilies = ge.getAvailableFontFamilyNames(); 
    Font fontT5 = new Font(Font.SANS_SERIF, Font.BOLD, 25); 
/////////// 

    public void runGUI() { 
     myMainWindow.setBounds(10, 10, 1296, 756); // don't guess the size (1) 
     myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     myMainWindow.setLayout(new GridLayout(1, 1)); 
     createFirstPanel(); 
     myMainWindow.getContentPane().add(firstPanel); 
     myMainWindow.pack(); // 1) Make it mininum size needed 
     myMainWindow.setMinimumSize(myMainWindow.getSize()); 
     myMainWindow.setVisible(true); 
    } 

    public void createFirstPanel() { 
     //firstPanel.setLayout(null); 
     firstPanel.setLayout(new GridLayout(1,0,50,50)); 
     firstPanel.setBorder(new EmptyBorder(50,50,50,50)); 
     Insets buttonMargin = new Insets(20, 20, 20, 20); 

     compAth.setText("<html><CENTER>Compare<br>Athletes</CENTER></html>"); 
     compAth.setMargin(buttonMargin); 
     compAth.setFont(fontT5); 
     firstPanel.add(compAth); 

     viewAth.setMargin(buttonMargin); 
     viewAth.setText("<html><CENTER>View<br>Athletes</CENTER></html>"); 
     viewAth.setFont(fontT5); 
     firstPanel.add(viewAth); 

     updateRD.setMargin(buttonMargin); 
     updateRD.setText("<html><CENTER>Update<br>Running<br>Details</CENTER></html>"); 
     updateRD.setFont(fontT5); 
     firstPanel.add(updateRD); 
    } 

    public static void main(String[] args) { 
     // should be on the EDT! 
     ResizeTst rt = new ResizeTst(); 
     rt.runGUI(); 
    } 
}