【问题标题】:creating a GUI in Java在 Java 中创建 GUI
【发布时间】: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


【解决方案1】:

与板球所说的不同,使用绝对位置会导致并给您带来很多问题。我建议您考虑使用布局管理器,例如 box layout,它看起来与您想要显示节点的方式完全相同。您需要做的就是在节点之间添加间距。你可以看看怎么做here.

因此,如果您仍然希望用户输入其框架的尺寸。您所要做的就是设置 jframe 的尺寸,所有东西都会自动调整。

【讨论】:

  • 我过去使用过它们,但是这对我来说是 A-Level 课程作业,我觉得如果我能够自己编写代码而不是使用布局管理器,那将会产生很大的不同,但是如果这是唯一的选择,我将使用它。
  • (1+) 用于使用布局管理器,尽管您应该查看 GridLayout,因为这将为每个组件提供相同的大小。有关工作示例,请参阅 Layout Managers 上的 Swing 教程。
猜你喜欢
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 2012-01-27
相关资源
最近更新 更多