JButton大小未被setLayout()更改。

huangapple go评论83阅读模式
英文:

JButton size not changed by setLayout()

问题

package com.myprojects;

import javax.swing.*;
import java.awt.*;
import java.awt.Font;

public class Main extends JFrame {

    public static void main(String[] args) {

        //frame
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();

        frame.setSize(1366, 768);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setIconImage(new ImageIcon("filedirectory").getImage());
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.add(panel);

        //panel
        panel.setBackground(new Color(34, 212, 187));
        Font titleFont = new Font("Batang", Font.BOLD, 120);
        Font font = new Font("Batang", Font.PLAIN, 12);

        //label
        JLabel title = new JLabel("Title");
        title.setBounds(10, 20, 80, 25);
        title.setFont(titleFont);
        title.setForeground(new Color(0xFFB700));
        panel.add(title);

        //play button
        JButton play = new JButton("Play");
        play.setFont(font);
        play.setBounds(500, 500, 250, 100);
        panel.add(play);

        frame.setVisible(true);
    }
}
英文:

I'm trying to learn Java and I decided to write my first program. I'm attempting to make a window with a title and a JButton. But the button position doesn't get changed by setBounds(), it still stays in one spot no matter what I change it to. I heard that adding frame.getcontentPane().setLayout(null) fixes this, but it doesn't.

Can you please tell me what the problem is? (line 37)

package com.myprojects;
import javax.swing.*;
import java.awt.*;
import java.awt.Font;
public class Main extends JFrame {
public static void main(String[] args) {
//frame
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setSize(1366,768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(new ImageIcon("filedirectory").getImage());
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.add(panel);
//panel
panel.setBackground(new Color(34, 212, 187));
Font titleFont = new Font("Batang", Font.BOLD,120);
Font font = new Font("Batang",Font.PLAIN, 12);
//label
JLabel title = new JLabel("Title");
title.setBounds(10,20,80,25);
title.setFont(titleFont);
title.setForeground(new Color(0xFFB700));
panel.add(title);
//play button
JButton play = new JButton("Play");
play.setFont(font);
play.setBounds(500,500,250,100);
panel.add(play);
frame.setVisible(true);
}

答案1

得分: 1

> 我决定编写我的第一个程序。

然后,您应该学习如何按照设计来使用 Swing。这意味着不要使用空布局。不要使用setBounds()!Swing 的设计是要与布局管理器一起使用的。默认情况下,JPanel 的布局管理器是FlowLayout。因此,您将在同一行上看到标签和按钮。

根据您的代码,看起来您想将标签放在框架的顶部,按钮放在底部。

因此,开始做以下操作:

  1. 摆脱 JPanel
  2. 使用以下方式将标签添加到框架:frame.add(title, BorderLayout.PAGE_START)
  3. 使用以下方式将按钮添加到框架:frame.add(play, BorderLayout.PAGE_END)

运行您的程序以查看效果。

框架内容面板的默认布局是BorderLayout。阅读 Swing 教程中关于布局管理器部分,以了解 add(...) 方法的第二个参数的含义。

如果您不喜欢这种布局,则可以将框架的布局管理器更改为其他布局管理器并进行实验。

英文:

> I decided to write my first program.

Then you should learn how to use Swing the way it was designed to be used. This means, DON"T use a null layout. Don't use setBounds()! Swing was designed to be used with layout managers. By default the layout manager for a JPanel is the FlowLayout. So you will see the label and the button on the same line.

Based on your code it looks like you are trying to put the label at the top of the frame and the button at the bottom.

So to get started:

  1. get rid of the JPanel
  2. Add the label to the frame using: frame.add(title, BorderLayout.PAGE_START)
  3. Add the button to the frame using: frame.add(play, BorderLayout.PAGE_END)

Run your program to see how this looks.

The default layout for the content pane of the frame is the BorderLayout. Read the section from the Swing tutorial on Layout Managers to understand what the second parameter for the add(...) method means.

If you don't like the layout, then change the layout manager of the frame to a different layout manager and experiment.

答案2

得分: 0

我会采取与camickr描述的方法略有不同的方法。当然仍然会使用布局,但会使用不同的布局来更接近代码似乎在描述的效果。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class ButtonCompoundLayout {
    public ButtonCompoundLayout() {
        //frame
        JFrame frame = new JFrame();
        //panel
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createTitledBorder("BorderLayout"));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);

        //label
        JLabel title = new JLabel("Title");
        title.setFont(title.getFont().deriveFont(50f));
        panel.add(title, BorderLayout.PAGE_START);
        title.setBorder(BorderFactory.createTitledBorder("BorderLayout.PAGE_START"));

        //play button
        JButton play = new JButton("Play");
        Insets insets = new Insets(20,30,30,30);
        play.setMargin(insets);
        JPanel controlPanel = new JPanel(new GridBagLayout());
        CompoundBorder compoundBorder = new CompoundBorder(
                BorderFactory.createTitledBorder("GridBagLayout"),
                new EmptyBorder(15, 50, 15, 50)
        );
        controlPanel.setBorder(compoundBorder);
        controlPanel.add(play);
        panel.add(controlPanel, BorderLayout.CENTER);

        frame.pack();
        frame.setMinimumSize(frame.getSize());
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            new ButtonCompoundLayout();
        };
        SwingUtilities.invokeLater(r);
    }
}

请仔细查看代码本身,并检查每个方法和类的空白,以下是一些建议/观察。

  • JFrame上调用setBounds似乎与setExtendedState(..)的调用相冲突。
  • 在无边框GUI中看不到图标图像,它只出现在标题栏上吗?
  • 设置颜色的选择有些奇怪。一个使用RGB整数,另一个使用十六进制值。使用一个即可。
  • 根据按钮的大小和位置,似乎意图是将其居中。但实际上它没有居中。
  • 不要假设字体在运行时可用,除非在应用程序中提供字体并在运行时加载它。
英文:

I'd take a somewhat different approach to that described by camickr. Still using layouts of course, but using different layouts that get closer to the effect the code seems to be describing.

JButton大小未被setLayout()更改。
JButton大小未被setLayout()更改。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class ButtonCompoundLayout {
public ButtonCompoundLayout() {
//frame
JFrame frame = new JFrame();
//panel
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(new TitledBorder("BorderLayout"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
//label
JLabel title = new JLabel("Title");
title.setFont(title.getFont().deriveFont(50f));
panel.add(title, BorderLayout.PAGE_START);
title.setBorder(new TitledBorder("BorderLayout.PAGE_START"));
//play button
JButton play = new JButton("Play");
Insets insets = new Insets(20,30,30,30);
play.setMargin(insets);
JPanel controlPanel = new JPanel(new GridBagLayout());
CompoundBorder compoundBorder = new CompoundBorder(
new TitledBorder("GridBagLayout"), 
new EmptyBorder(15, 50, 15, 50)
);
controlPanel.setBorder(compoundBorder);
controlPanel.add(play);
panel.add(controlPanel, BorderLayout.CENTER);
frame.pack();
frame.setMinimumSize(frame.getSize());
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable r = () -> {
new ButtonCompoundLayout();
};
SwingUtilities.invokeLater(r);
}
}

Carefully review the code itself and check each of the methods and classes used for white-space, but here are some general tips / observations.

  • The call to setBounds on the JFrame seemed to be fighting the call to setExtendedState(..)
  • An icon image won't be seen in an undecorated GUI when it is on-screen, was it only for the title bar?
  • Odd choice of setting the colors. One is specified as RGB integers, the other as a Hex value. Use one or the other.
  • By the size and position of the button it appears the intent was to center it. It wasn't centered.
  • Don't presume fonts are available unless supplying the font with the app. and loading it at runtime.

答案3

得分: 0

你不应该使用空布局,因为它无法适应不同的屏幕尺寸。我建议你应该学习如何使用布局。例如,每次我设计界面时我都使用GridBagLayout。

英文:

You should never use a null layout, because it does not adapt to different screen sizes. I suggest you should learn how to use a layout. For example, I use GridBagLayout everytime I design a gui.

huangapple
  • 本文由 发表于 2020年8月15日 07:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63421131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定