java面板的大小_java – 单击按钮更改面板大小

我有以下代码:

package in.res.num.tapb.ui;

import java.awt.BorderLayout;

import java.awt.CardLayout;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

class MainClass extends JPanel {

public MainClass() {

Registration registration = new Registration();

ButtonPanel buttonPanel = new ButtonPanel();

buttonPanel.setRegistration(registration);

buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));

registration.setBorder(BorderFactory.createTitledBorder("Registration Panel"));

setLayout(new BorderLayout());

add(registration, BorderLayout.CENTER);

add(buttonPanel, BorderLayout.SOUTH);

}

private static void createAndShowUI() {

JFrame frame = new JFrame("Registration");

frame.getContentPane().add(new MainClass());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setResizable(false);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

createAndShowUI();

}

});

}

@SuppressWarnings("serial")

private class ButtonPanel extends JPanel {

private Registration registration;

public ButtonPanel() {

setLayout(new GridLayout(1, 0, 10, 0));

for (final String keyText : Registration.KEY_TEXTS) {

JButton btn = new JButton(keyText);

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (registration != null) {

registration.swapView(keyText);

}

}

});

add(btn);

}

}

public void setRegistration(Registration registration) {

this.registration = registration;

}

}

private static class Registration extends JPanel {

private static final Dimension PREF_SIZE = new Dimension(450, 300);

public static final String USER_AGREEMENT = "User Agreement";

public static final String USER_INFO = "User Information";

public static final String ENROLLMENT = "Enrollment";

public static final String[] KEY_TEXTS = { USER_AGREEMENT, USER_INFO, ENROLLMENT };

private CardLayout cardlayout = new CardLayout();

private JPanel cards = new JPanel(cardlayout);

public Registration() {

cards.add(createUserAgreePanel(), USER_AGREEMENT);

cards.add(createUserInfoPanel(), USER_INFO);

cards.add(createEnrollmentPanel(), ENROLLMENT);

setLayout(new BorderLayout());

add(cards, BorderLayout.CENTER);

}

private JPanel createEnrollmentPanel() {

JPanel enrol = new JPanel();

enrol.setSize(new Dimension(400, 200));

enrol.add(new JLabel("Enrollment"));

return enrol;

}

private JPanel createUserAgreePanel() {

JPanel userAgree = new JPanel();

userAgree.setSize(new Dimension(200, 300));

userAgree.add(new JLabel("User Agreement"));

return userAgree;

}

private JPanel createUserInfoPanel() {

JPanel userInfo = new JPanel();

userInfo.setSize(new Dimension(300, 400));

userInfo.add(new JLabel("User Information"));

return userInfo;

}

public void swapView(String key) {

cardlayout.show(cards, key);

}

}

}

如您所见,我想更改按钮单击时的大小.可能吗?上面的代码不起作用,我的意思是大小不变.如何在飞行中改变大小?

感谢致敬.

编辑:

在选择一行JList时交换面板.

getChoicesList().addListSelectionListener(new ListSelectionListener() {

@Override

public void valueChanged(ListSelectionEvent listSelectionEvent) {

getViewPanel().changeView(getChoicesList().getSelectedIndex());

getChoicePanel().changeView(Constants.PanelInfo.valueOf(getEngine().getChoiceList().get(getChoicesList().getSelectedIndex()).getEnumName()).getDimensionForScrollPaneOfChoicePanel());

((MainFrame) getTopLevelAncestor()).pack();

}

});

ViewPanel#changeView(),这会交换面板:

public void changeView(int index) {

removeAll();

getPanels().get(index).setPreferredSize(Constants.PanelInfo.valueOf(getEngine().getChoiceList().get(index).getEnumName()).getDimensionForViewPanel());

add(getPanels().get(index));

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值