2016-08-02 104 views
-1

我已經在java中創建基本的GUI。簡而言之,我試圖實現的是給定字體中的標題,並且低於包含等間隔和大小的5個不同按鈕的標題,並具有修改器功能,該修改器功能允許通過比例因子改變分辨率0.75,JPanel不會觸摸屏幕的邊緣,但有20個像素的邊界。我希望實現的是用戶能夠輸入他們想要的任何分辨率,並且仍然保持相同的基本設計,從而使其與其他設備兼容的方式。在Java中創建GUI

GUI design

我一直使用至今的代碼是:

package prototype1; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class GUI { 


public static void main(String[] args){ 
    stateManager(); 
} 

static public JFrame menu = new JFrame("Menu Screen"); 
static public double modifier = 1; 
static public int width = 750; 
static public int height = 1334; 

static void stateManager(){ 

    JFrame.setDefaultLookAndFeelDecorated(true); 

    AL demo = new AL(); 
    menu.setContentPane(demo.contentPanel1()); 

    menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    menu.setSize((int) (width * modifier),(int) (height * modifier)); 
    System.out.println((int)(width * modifier)); 
    menu.setVisible(true); 
} 


static class AL implements ActionListener{ 
    JLabel titleLabel; 
    JButton checkStar; 
    JPanel buttonScreenMenu; 

    public JPanel contentPanel1(){ 
     JPanel totalGUI = new JPanel(); 
     totalGUI.setLayout(null); 
     totalGUI.setBackground(Color.white); 

     int x = width; 
     int y = height; 

     titleLabel = new JLabel("Select what it is you would like to do!"); 
     titleLabel.setFont(new Font("Castellar",Font.PLAIN, (int) (18 * modifier))); 
     titleLabel.setLocation(0,(int) (40 * modifier)); 
     titleLabel.setSize((int) (x * modifier),(int) (30 * modifier)); 
     titleLabel.setHorizontalAlignment(0); 
     totalGUI.add(titleLabel); 

     buttonScreenMenu = new JPanel(); 
     buttonScreenMenu.setLocation((int) (20 * modifier) , (int) (100 * modifier)); 
     buttonScreenMenu.setSize((int) ((x - 40) * modifier),(int) ((y - 120) * modifier)); 
     buttonScreenMenu.setBorder(BorderFactory.createLineBorder(Color.black)); 
     totalGUI.add(buttonScreenMenu); 

     checkStar = new JButton("Work out a Star"); 
     checkStar.setLocation((int)(20 * modifier),(int)(20 * modifier)); 
     checkStar.setSize(buttonScreenMenu.getWidth() - 40, (int) (buttonScreenMenu.getHeight()/4) - 40); 
     checkStar.setBackground(Color.white); 
     buttonScreenMenu.add(checkStar); 



     return totalGUI; 


    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 
} 


} 

但是我停在這裏,因爲我沒有得到我附近想要的結果的任何地方。這段代碼的輸出如下:

output GUI

如何解決分辨率問題?

+1

你應該看看Swing提供的各種佈局管理器 –

+0

我的建議是使用GUI Swing構建器。 – m0skit0

回答

1

關於蟋蟀說,使用絕對位置會導致並給你很多問題。我建議你看看使用佈局管理器,如box layout,它看起來與你想要顯示節點的方式完全相同。您只需在節點之間添加間距即可。您可以看到如何操作here.

因此,或者如果您仍希望用戶輸入框架的尺寸。所有你需要做的就是設置你的jframe的尺寸,每件事情都會自動調整。

+0

我過去曾使用過它們,但是這對我來說是一門高級課程,我覺得如果我能夠自己編寫代碼而不是使用佈局管理器,它會產生很大的變化,但如果這是唯一的選擇,我會將會使用它。 –

+0

(1+)使用佈局管理器,但您應該查看GridLayout,因爲這將提供每個組件的相同大小。有關工作示例,請參見[佈局管理器](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)上的Swing教程。 – camickr