frame.setMaximumSize(new Dimension(400,400));为什么没效果

Haisen大王 2015-06-01 03:09:31
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test {
private static void createAndShowGUI(){
JFrame frame=new JFrame("FlowLayout Demo");
frame.setMinimumSize(new Dimension(200,200));
frame.setMaximumSize(new Dimension(400,400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}
...全文
132 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
alan19931103 2015-06-01
  • 打赏
  • 举报
回复
顶层窗口的位置和大小受桌面窗口管理系统的控制。这个方法的调用是转发到窗口管理系统的一个请求(不是命令),它会尽量响应这个请求。但是某些情况下他会忽略这个请求,使之与桌面设置相匹配。 没试过,不行的话你可以自己实现一个,比期望的窗口大了强制缩小
傻傻de点点 2015-06-01
  • 打赏
  • 举报
回复
这个问题没法回答,从JDK1.6开始我就发现这问题了,查了几年了,一直没有一条准确的答案。 http://bugs.java.com/view_bug.do?bug_id=6464548看到官网提交的BUG也并没有人理。
手工写的旅店管理系统界面。 import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Set; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JRadioButton; import javax.swing.JTextField; public class MyButton extends JButton implements MouseListener, ActionListener { private static final long serialVersionUID = 6261021704484837560L; private JPopupMenu popMenu; private ButtonGroup bg; private JRadioButton SetDark;// 置为脏房,待打扫 private JRadioButton SetClean;// 置为应该打扫的房间 private JRadioButton SetUsing;// 置为正在使用的房间 private JRadioButton SetHouseNum;// 设置房号 private JRadioButton SetEmpty;//置为空房 private JRadioButton Delete;//删除 private JRadioButton Bujiaoyajin;//补交押金 private String HouseAr, HouseNu; private Color Housecolor; private int ID; private double SingelSal; public double getSingelSal() { return SingelSal; } public void setSingelSal(double singelSal) { SingelSal = singelSal; } private HashMap tempMap; private Customer customer; public MyButton(int ID,HashMap tempMap) { super.setIcon(new ImageIcon("Blue Victorian.JPG")); this.ID = ID; this.tempMap = tempMap; init(); } public MyButton() { super.setIcon(new ImageIcon("Blue Victorian.JPG")); init(); } public void init() { Housecolor = Color.GREEN; super.setBackground(Housecolor); popMenu = new JPopupMenu(); bg = new ButtonGroup(); SetDark = new JRadioButton("置为脏房"); SetClean = new JRadioButton("置为闲置房"); SetUsing = new JRadioButton("添加留客"); SetHouseNum = new JRadioButton("初始房态"); SetEmpty = new JRadioButton("置为空房"); Delete = new JRadioButton("删除本房"); Bujiaoyajin = new JRadioButton("补交押金"); // 添加action监听 SetDark.addActionListener(this); SetClean.addActionListener(this); SetUsing.addActionListener(this); SetHouseNum.addActionListener(this); SetEmpty.addActionListener(this); Delete.addActionListener(this); Bujiaoyajin.addActionListener(this); // 加入buttongroup,实现单选 bg.add(SetClean); bg.add(SetDark); bg.add(SetUsing); bg.add(SetHouseNum); bg.add(SetEmpty); bg.add(Delete); bg.add(Bujiaoyajin); //bg.add(Delete); // 设置相应房态颜色 popMenu.add(SetDark); popMenu.add(SetClean); popMenu.add(SetUsing); popMenu.add(SetHouseNum); popMenu.add(SetEmpty); popMenu.add(Bujiaoyajin); //popMenu.add(Delete); // 设置jbutton的固定大小 setMaximumSize(new Dimension(80, 60)); setMinimumSize(new Dimension(80, 60)); setPreferredSize(new Dimension(80, 60)); this.addMouseListener(this); } public void mouseClicked(MouseEvent arg0) {} public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mouseReleased(MouseEvent arg0) {} // 为jbuuton添加popmenu public void mousePressed(MouseEvent arg0) { if (arg0.getButton() == MouseEvent.BUTTON3) { popMenu.show(arg0.getComponent(), arg0.getX(), arg0.getY()); } } //定位该button在map中的位置 public MyButton indexof() { MyButton b = null; Set set = this.tempMap.keySet(); Iterator iter = set.iterator(); while(iter.hasNext()) { String id = iter.next(); if(this.ID == Integer.parseInt(id.trim())) b = this.tempMap.get(id); } return b; } //getcustomer的信息 public Customer getCustomer() { return this.customer; } // 为选项添加监听 public void actionPerformed(ActionEvent e) { if(e.getSource() == Bujiaoyajin) { System.out.println("fgfhjhk"); } if (e.getSource() == SetDark) { Housecolor = Color.yellow; this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); Write(); } if (e.getSource() == SetClean) { Housecolor = Color.gray; this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); Write(); } if (e.getSource() == Delete) { tempMap.remove(ID); Write(); } if (e.getSource() == SetEmpty) { Housecolor = Color.green; this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); Write(); } if (e.getSource() == SetUsing) { new GetCustomerInfo(); this.setBackground(Housecolor); } if (e.getSource() == SetHouseNum) { new GetHouseInfo(); } } public int getID() { return ID; } public void setID(int id) { ID = id; } //保存到本地 public void Write() { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/ButtonsInfo.shf")); oos.writeObject(this.tempMap); oos.flush(); oos.close(); } catch (Exception e) { e.printStackTrace(); } } //为外层程序提供 public void setHouseAr(String str) { this.HouseAr = str; } public String getHouseAr() { return this.HouseAr; } public void setHouseNu(String str) { this.HouseNu = str; } public String getHouseNu() { return this.HouseNu; } public void setHouseColor(Color cl) { this.Housecolor = cl; } public Color getHouseColor() { return this.Housecolor; } // 设置房号的窗口 class GetHouseInfo extends JFrame implements ActionListener { private static final long serialVersionUID = 4824740927444846504L; private JPanel p, p1, p2, p3,p4; private JLabel HouseArea, HouseNum,SingleSal; private JTextField HouseA, HouseN,SinglS; private JButton Entrue, Cansal; private String A,N; public GetHouseInfo() { super.setTitle("初始房态"); p = new JPanel(); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); HouseArea = new JLabel("设定房区:"); HouseNum = new JLabel("设定房号:"); SingleSal = new JLabel("设定单价:"); HouseA = new JTextField(10); HouseN = new JTextField(10); SinglS = new JTextField(10); Entrue = new JButton("确定"); Cansal = new JButton("取消"); Entrue.addActionListener(this); Cansal.addActionListener(this); p.setLayout(new GridLayout(4, 1)); p1.add(HouseArea); p1.add(HouseA); p2.add(HouseNum); p2.add(HouseN); p3.add(Entrue); p3.add(Cansal); p4.add(SingleSal); p4.add(SinglS); p.add(p1); p.add(p2); p.add(p4); p.add(p3); this.add(p); this.setLocation(450, 300); this.setAlwaysOnTop(true); this.setVisible(true); this.pack(); } public String getHouseA() { return this.A; } public String getHouseN() { return this.N; } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == Entrue) { A = HouseAr = HouseA.getText(); N = HouseNu = HouseN.getText(); SingelSal = new Double(SinglS.getText().trim()).doubleValue(); MyButton.this.setText(HouseAr + "\n" + HouseNu); indexof().setHouseNu(A+N); Write(); this.hide(); } if (arg0.getSource() == Cansal) { this.hide(); } } } //留客信息录入 class GetCustomerInfo extends JFrame implements ActionListener { private static final long serialVersionUID = 4824740927444846504L; private JPanel p, p1, p2, p3,p4,p5,p6; private JLabel Name, SumCash,SumDays,Sex,TelePhone; private JTextField HouseA, HouseN,SumD,telePhone; private JButton Entrue, Cansal; private JLabel sex; private ButtonGroup bg; private JRadioButton man,waman; private String sexStr; public GetCustomerInfo() { super.setTitle("录入顾客信息"); p = new JPanel(); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); p5 = new JPanel(); p6 = new JPanel(); Name = new JLabel("旅客姓名:"); sex = new JLabel("性别: "); TelePhone = new JLabel("联系方式:"); SumCash = new JLabel("旅客押金:"); SumDays = new JLabel("留住天数:"); HouseA = new JTextField(10); telePhone = new JTextField(10); HouseN = new JTextField(10); SumD = new JTextField(10); Entrue = new JButton("确定"); Cansal = new JButton("取消"); bg = new ButtonGroup(); man = new JRadioButton("男"); waman = new JRadioButton("女"); bg.add(man); bg.add(waman); p6.add(sex); p6.add(man); p6.add(waman); Entrue.addActionListener(this); Cansal.addActionListener(this); man.addActionListener(this); waman.addActionListener(this); p.setLayout(new GridLayout(6, 1)); p1.add(Name); p1.add(HouseA); p2.add(SumCash); p2.add(HouseN); p3.add(Entrue); p3.add(Cansal); p4.add(SumDays); p4.add(SumD); p5.add(TelePhone); p5.add(telePhone); p.add(p1); p.add(p5); p.add(p6); p.add(p2); p.add(p4); p.add(p3); this.add(p); this.setLocation(450, 300); this.setVisible(true); this.pack(); } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0) { if(arg0.getSource() == man) { sexStr = "男"; } if(arg0.getSource() == waman) { sexStr = "女"; } if (arg0.getSource() == Entrue) { customer = new Customer(HouseA.getText(),sexStr,telePhone.getText(), new Integer(SumD.getText().trim()).intValue(), new Integer(HouseN.getText().trim()).intValue()); Housecolor = Color.blue; MyButton.this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); if(new Double(HouseN.getText().trim()).doubleValue() < SingelSal) { JOptionPane.showMessageDialog(null, "押金不足!"); } else { Write(); this.hide(); } } if (arg0.getSource() == Cansal) { this.hide(); } } } public static void main(String[] s) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); MyButton mb = new MyButton(); panel.add(mb); frame.add(panel); frame.setSize(600, 600); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧