1

我只是在增加关于 GUI 和 GUI 和按钮的实现的知识,我遇到了一个小问题。我已经搜索过这个,甚至比较了按钮代码,但与其他人的代码相比,我所看到的似乎没有什么不同。当我将 html 添加到任何按钮时,以 testOne 为例,如果我使用:

JButton testOne = new JButton("<html><b><u>T</u>est<br>1</b></html>");        

当然,我有一个按钮,但是这个按钮太大了,字体很小,而我的其他按钮是正常大小的。有人知道为什么吗?我发布了没有作业标签的代码,因为它不是作业,只是有趣的练习。

public FunProject(){
    super("Fun Project"); //child
    //create layout and get the content pane for content
    contents = getContentPane();
    contents.setLayout(new FlowLayout(FlowLayout.CENTER));
    leftBox = getContentPane();
    leftBox.setLayout(new BoxLayout(leftBox, BoxLayout.X_AXIS));
    //menuBar = new 
    //Create the buttons to use
    JButton testOne = new JButton("Test 1");
    JButton testTwo = new JButton("Test 2");
    JButton testThree = new JButton("Test 3");

此外,对于任何想传播一点知识的人,我将如何堆叠我的按钮?现在它们只是在一个未堆叠的水平面上,但在我想要的位置(左边的中心)。先感谢您。

import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import java.awt.*;
import java.awt.event.*;

public class FunProject extends JFrame implements ActionListener, KeyListener,     MenuListener{
//Content only visible to me
    private Container contents;
    private Container leftBox;
    private JButton testOne, testTwo, testThree;
    private JMenuBar menuBar;
    private JMenu men, file, edit, exit;
    private JMenuItem fileOpen, fileSave, fileType;
    private JMenuItem editOpen, editSave, editType;


//Constructor
public FunProject(){
    super("Fun Project");
    //create layout and get the content pane for content
    contents = getContentPane();
    contents.setLayout(new FlowLayout(FlowLayout.CENTER));
    leftBox = getContentPane();
    leftBox.setLayout(new BoxLayout(leftBox, BoxLayout.X_AXIS));


    //Key Listener
    this.addKeyListener(this);

    //Creating Menu bar
    menuBar = new JMenuBar();

    //Adding header to menuBar
    men = new JMenu("Test Menu");
    men.addMenuListener(this);
    menuBar.add(men);

    //Adding exit to menuBar
    exit = new JMenu("Exit Menu");
    exit.setMnemonic(KeyEvent.VK_X);
    exit.addMenuListener(this);
    menuBar.add(exit);

    //Add submenu
    file = new JMenu("FILE");
    file.addMenuListener(this);
    men.add(file);

    edit = new JMenu("EDIT");
    edit.addMenuListener(this);
    men.add(edit);

    //Add item to submenu FILE
    fileOpen = new JMenuItem("Open a file");
    fileOpen.addActionListener(this);
    file.add(fileOpen);

    fileSave = new JMenuItem("Save a file");
    fileSave.addActionListener(this);
    file.add(fileSave);

    fileType = new JMenuItem("File type");
    fileType.addActionListener(this);
    file.add(fileType);

    //Add item to submenu EDIT
    editOpen = new JMenuItem("Open a file");
    editOpen.addActionListener(this);
    edit.add(editOpen);

    editSave = new JMenuItem("Save a file");
    editSave.addActionListener(this);
    edit.add(editSave);

    edit = new JMenuItem("File type");
    editType.addActionListener(this);
    edit.add(editType);

    //Add menu bar
    this.setJMenuBar(menuBar);

    //Create the buttons to use
    JButton testOne = new JButton("Test 1"); 
    JButton testTwo = new JButton("Test 2");
    JButton testThree = new JButton("Test 3");
    //Button modifications
    testOne.setPreferredSize(new Dimension(100, 100));
    //Add buttons and such to the leftBox (frame > pane)
    leftBox.add(testOne);
    leftBox.add(testTwo);
    leftBox.add(testThree);

    //Tell the program what Listener will handle events for the button or whatever you
    //are implementing
    testOne.addActionListener(this);
    testTwo.addActionListener(this);
    testThree.addActionListener(this);

    //Set size and visibility to display content and color
    setSize(400,300);
    setVisible(true);

}
public static void main(String[] args){
    FunProject fp = new FunProject();
    fp.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

我在搞乱菜单(所以忽略菜单代码)和它们的全部内容。那是为了以后我更好地学习它。

4

1 回答 1

0

1)尝试使用

h1 标签 ->JButton testOne = new JButton("<html><b><u><h1>T</u>est<br>1</b></html>");

增加大小或字体使用<font>标签来改变字体的颜色或大小。

2)有时它取决于您使用的布局检查一下

3) 尝试 ButtonObject.setBounds();

于 2013-09-15T12:32:40.643 回答