java漂亮按钮,Java摇摆。从JButton打开新的JPanel并使按钮漂亮

I am trying to build a little program that has a main GUI with 2 buttons. One button closes the program, the other I want to open a new JPanel that will have text fields etc.

I would like to be able to make the buttons so they look like normal application buttons I guess, nice and square, equal size etc. etc., I am not sure how to do this though.

Also, I am unsure how to open a new JFrame from a button click.

GUI Code:

package practice;

public class UserInterface extends JFrame {

private JButton openReportSelection = new JButton("Open new Window");

private JButton closeButton = new JButton("Close Program");

private JButton getCloseButton() {

return closeButton;

}

private JButton getOpenReportSelection() {

return openReportSelection;

}

public UserInterface() {

mainInterface();

}

private void mainInterface() {

setTitle("Program Information Application");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel centerPanel = new JPanel(new GridLayout(0, 3));

centerPanel.add(openReportSelection);

centerPanel.add(closeButton);

getCloseButton().addActionListener(new Listener());

add(centerPanel, BorderLayout.CENTER);

setSize(1000, 200);

setVisible(true);

}

private void addReportPanel() {

JPanel reportPanel = createNewPanel();

getContentPane().add(reportPanel, BorderLayout.CENTER);

}

private JPanel createNewPanel() {

JPanel localJPanel = new JPanel();

localJPanel.setLayout(new FlowLayout());

return localJPanel;

}

}

ActionListener Class code:

package practice;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class Listener implements ActionListener {

public void actionPerformed(ActionEvent ae) {

System.exit(0);

}

}

EDIT: I think opening a new JPanel would be the way to go rather than a JFrame. What would be the best way to do this from a Jbutton click?

解决方案

Start by using a different layout manager, FlowLayout or GridBagLayout might work better

JPanel centerPanel = new JPanel(new FlowLayout());

centerPanel.add(openReportSelection);

centerPanel.add(closeButton);

These layouts will honour the preferred sizes of your buttons

As for opening another window, well, you've already create one, so the process is pretty much the same. Having said that, you might consider having a look at The Use of Multiple JFrames: Good or Bad Practice? before you commit yourself to far.

A better approach might be to use a JMenuBar and JMenuItems to act as the "open" and "exit" actions. Take a look at How to Use Menus then you could use a CardLayout to switch between views instead, for example

From a pure design perspective (I know it's only practice, but perfect practice makes perfect), I wouldn't extend anything from JFrame and instead would rely on building your main GUIs around something like JPanel instead.

This affords you the flexibility to decide how to use these components, as you could add them to frames, applets or other components...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值