简体   繁体   English

如何正确调整JFrame中按钮的大小和位置?

[英]How can I properly adjust the size and location of a button in JFrame?

I am attempting to make a PC Application using Java and JFrame . 我正在尝试使用Java和JFrame制作PC应用程序。 I'm trying to format 2 transparent buttons, each sized half of the full screen shown (vertically). 我正在尝试格式化2个透明按钮,每个按钮的大小是垂直显示的全屏的一半。 The top half of the screen will hold to option to debate someone and the bottom half of the screen will hold the option to spectate a debate if clicked on. 屏幕的上半部分将保留辩论某人的选项,而屏幕的下半部分将保留观看某项辩论的选项(如果单击该选项)。 Here is what I have so far: 这是我到目前为止的内容:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame {
   JButton b1;
   JButton b2;
   JPanel j1;
   JPanel j2;

   public BackgroundImageJFrame() {
      setTitle("Background Color for JFrame");
      setSize(340,563);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
      setLayout(null);
/*
    One way
    -----------------
    setLayout(new BorderLayout());
    JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
    add(background);
    background.setLayout(new FlowLayout());
    l1=new JLabel("Here is a button");
    b1=new JButton("I am a button");
    background.add(l1);
    background.add(b1);
*/
// Another way
      setLayout(new BorderLayout());
      setContentPane(new JLabel(new ImageIcon("C:\\Users\\MLH-User\\Downloads\\Front.jpg")));
      setLayout(new FlowLayout());
      j1 = new JPanel();
      j1.setLayout(null);
      b1 = new JButton("Spectate");
      //b1.setBounds(0,0,50,50);
      b1.setOpaque(false);
      b1.setContentAreaFilled(false);
      b1.setBorderPainted(false);
      j1.add(b1);

      b2 = new JButton("Debate");



      b2.setLocation(0,0);
      b2.setOpaque(false);
      b2.setContentAreaFilled(false);
      b2.setBorderPainted(false);
      j1.add(b2);
      add(j1);
    // Just for refresh :) Not optional!
      setSize(339,562);
      setSize(340,563);
    }
    public static void main(String args[])   {
      new BackgroundImageJFrame();
    }
} 

This is some stuff I experimented with so far, can anyone help me out about where I went wrong? 到目前为止,这是我尝试过的一些东西,有人可以帮助我解决我的错误之处吗?

在此处输入图片说明

You should use a layout manager. 您应该使用布局管理器。 Here is an example with GridLayout : 这是GridLayout的示例:

在此处输入图片说明

public class Example extends JFrame {

    private static final int SIZE = 300;

    public Example() {

        setLayout(new GridLayout(2, 1, 0, 5));
        getContentPane().setBackground(Color.WHITE);

        JButton debate = new JButton("DEBATE") {

            public Dimension getPreferredSize() {

                return new Dimension(SIZE, SIZE);
            }
        };
        Font font = debate.getFont().deriveFont(30f);
        debate.setFont(font);
//      debate.setBorderPainted(false);
        debate.setBackground(Color.BLUE.brighter());
        debate.setForeground(Color.WHITE);

        JButton spectate = new JButton("SPECTATE") {

            public Dimension getPreferredSize() {

                return new Dimension(SIZE, SIZE);
            }
        };
        spectate.setFont(font);
//      spectate.setBorderPainted(false);
        spectate.setBackground(Color.RED.brighter());
        spectate.setForeground(Color.WHITE);

        add(debate);
        add(spectate);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(final String[] args) {

        SwingUtilities.invokeLater(() -> new Example());
    }
}

Notes: 笔记:

  • You have to realize that screen sizes vary. 您必须意识到屏幕尺寸会有所不同。 Setting SIZE to 300 was an arbitrary choice for presentation, screens might not have the required size. SIZE设置为300是演示的任意选择,屏幕可能没有所需的尺寸。 You can also set the insets or an empty border instead of specifying the size of the component directly. 您也可以设置插图或空白边框,而不用直接指定组件的大小。
  • You can consider creating a class for these buttons if you have more of them. 如果有更多按钮,则可以考虑为这些按钮创建一个类。

This is an example of setting the sizes. 这是设置尺寸的示例。 I don't know about the location part though. 我不知道位置部分。

    JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4,4,4,4));

for(int i=0 ; i<16 ; i++){
    JButton btn = new JButton(String.valueOf(i));
    btn.setPreferredSize(new Dimension(40, 40));
    panel.add(btn);
}
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM