【发布时间】:2016-12-07 20:11:38
【问题描述】:
我在java中创建了基本的GUI。很简单,我想要实现的是给定字体的标题,下面是一个 JPanel 包含 5 个等距和大小的不同按钮,具有允许通过比例因子更改分辨率的修改器功能比如说 0.75,JPanel 不会触及屏幕边缘,但有 20 个像素的边框。我希望实现的是一种用户能够输入他们想要的任何分辨率并使其仍然保持相同的基本设计从而使其与其他设备兼容的方式。
。
我目前使用的代码是:
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 构建器。
标签: java swing user-interface