我已經在java中創建基本的GUI。簡而言之,我試圖實現的是給定字體中的標題,並且低於包含等間隔和大小的5個不同按鈕的標題,並具有修改器功能,該修改器功能允許通過比例因子改變分辨率0.75,JPanel
不會觸摸屏幕的邊緣,但有20個像素的邊界。我希望實現的是用戶能夠輸入他們想要的任何分辨率,並且仍然保持相同的基本設計,從而使其與其他設備兼容的方式。在Java中創建GUI
。
我一直使用至今的代碼是:
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.
}
}
}
但是我停在這裏,因爲我沒有得到我附近想要的結果的任何地方。這段代碼的輸出如下:
如何解決分辨率問題?
你應該看看Swing提供的各種佈局管理器 –
我的建議是使用GUI Swing構建器。 – m0skit0