我只是提高了我对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);
}
我正在弄乱菜单(所以忽略菜单代码)以及它们的全部内容。这是我以后更好地学习它的时间。
答案 0 :(得分:0)
1)尝试使用
h1标签 - &gt;
JButton testOne = new JButton("<html><b><u><h1>T</u>est<br>1</b></html>");
用于增加大小或字体使用<font>
标记以更改字体的颜色或大小。
2)有些时候它取决于您使用的布局检查出来
3)尝试ButtonObject.setBounds();