Java GUI问题,求大神解决,谢了 (为什么我的布局显示不出来?)
packagecom.cqeec.sc;importjava.awt.*importjavax.swing.*publicclassSimpleCalcuatorFram...
package com.cqeec.sc;
import java.awt.*
import javax.swing.*
public class SimpleCalcuatorFrame extends JFrame {
public SimpleCalcuatorFrame() {
JFrame jframe = new JFrame("简易计算器 v1.0");
jframe.setSize(400, 250);
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setAlwaysOnTop(true);
jframe.setResizable(false);
JMenuBar editMenu = new JMenuBar();
jframe.setJMenuBar(editMenu);
JMenu jmenu = new JMenu("编辑");
editMenu.add(jmenu);
JMenuItem copyMenuItem = new JMenuItem("复制");
jmenu.add(copyMenuItem);
JSeparator s = new JSeparator();
jmenu.add(s);
JMenuItem pMenu = new JMenuItem("粘贴");
jmenu.add(pMenu);
JMenu viewMenu = new JMenu("查看");
editMenu.add(viewMenu);
JMenuItem kx = new JMenuItem("科学型");
viewMenu.add(kx);
JMenuItem bz = new JMenuItem("标准型");
viewMenu.add(bz);
JMenu bzcd = new JMenu("帮助");
editMenu.add(bzcd);
JMenuItem bangzu = new JMenuItem("帮助");
bzcd.add(bangzu);
JMenuItem gy = new JMenuItem("关于");
bzcd.add(gy);
BorderLayout borderlayout = new BorderLayout();
setLayout(borderlayout);
Button btnEast = new Button("don");
Button btnWest = new Button("don");
Button btnNorth = new Button("don");
Button btnSouth = new Button("don");
Button btnCenter = new Button("don");
add(btnEast,BorderLayout.EAST);
add(btnWest,BorderLayout.WEST);
add(btnNorth,BorderLayout.NORTH);
add(btnSouth,BorderLayout.SOUTH);
add(btnCenter,BorderLayout.CENTER);
jframe.setVisible(true);
}
public static void main(String[] args) {
SimpleCalcuatorFrame sc = new SimpleCalcuatorFrame();
}
} 展开
import java.awt.*
import javax.swing.*
public class SimpleCalcuatorFrame extends JFrame {
public SimpleCalcuatorFrame() {
JFrame jframe = new JFrame("简易计算器 v1.0");
jframe.setSize(400, 250);
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setAlwaysOnTop(true);
jframe.setResizable(false);
JMenuBar editMenu = new JMenuBar();
jframe.setJMenuBar(editMenu);
JMenu jmenu = new JMenu("编辑");
editMenu.add(jmenu);
JMenuItem copyMenuItem = new JMenuItem("复制");
jmenu.add(copyMenuItem);
JSeparator s = new JSeparator();
jmenu.add(s);
JMenuItem pMenu = new JMenuItem("粘贴");
jmenu.add(pMenu);
JMenu viewMenu = new JMenu("查看");
editMenu.add(viewMenu);
JMenuItem kx = new JMenuItem("科学型");
viewMenu.add(kx);
JMenuItem bz = new JMenuItem("标准型");
viewMenu.add(bz);
JMenu bzcd = new JMenu("帮助");
editMenu.add(bzcd);
JMenuItem bangzu = new JMenuItem("帮助");
bzcd.add(bangzu);
JMenuItem gy = new JMenuItem("关于");
bzcd.add(gy);
BorderLayout borderlayout = new BorderLayout();
setLayout(borderlayout);
Button btnEast = new Button("don");
Button btnWest = new Button("don");
Button btnNorth = new Button("don");
Button btnSouth = new Button("don");
Button btnCenter = new Button("don");
add(btnEast,BorderLayout.EAST);
add(btnWest,BorderLayout.WEST);
add(btnNorth,BorderLayout.NORTH);
add(btnSouth,BorderLayout.SOUTH);
add(btnCenter,BorderLayout.CENTER);
jframe.setVisible(true);
}
public static void main(String[] args) {
SimpleCalcuatorFrame sc = new SimpleCalcuatorFrame();
}
} 展开
展开全部
首先
import java.awt.*;//加上分号
import javax.swing.*;//加上分号
然后有两种修改方式
第一,去掉继承于JFrame类。然后add的时候
//BorderLayout borderlayout = new BorderLayout();
//setLayout(borderlayout);
//删除上面两行,因为JFrame默认就是BorderLayout布局
jframe.add(btnEast, BorderLayout.EAST);
jframe.add(btnWest, BorderLayout.WEST);
jframe.add(btnNorth, BorderLayout.NORTH);
jframe. add(btnSouth, BorderLayout.SOUTH);
jframe. add(btnCenter, BorderLayout.CENTER);
第二种:去掉新建窗口
//JFrame jframe = new JFrame("简易计算器 v1.0");
jframe.setXXX//删除jfram.
完整的参考代码
import java.awt.*;
import javax.swing.*;
public class SimpleCalcuatorFrame {
public SimpleCalcuatorFrame() {
JFrame jframe = new JFrame("简易计算器 v1.0");
jframe.setSize(400, 250);
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setAlwaysOnTop(true);
jframe.setResizable(false);
JMenuBar editMenu = new JMenuBar();
jframe.setJMenuBar(editMenu);
JMenu jmenu = new JMenu("编辑");
editMenu.add(jmenu);
JMenuItem copyMenuItem = new JMenuItem("复制");
jmenu.add(copyMenuItem);
JSeparator s = new JSeparator();
jmenu.add(s);
JMenuItem pMenu = new JMenuItem("粘贴");
jmenu.add(pMenu);
JMenu viewMenu = new JMenu("查看");
editMenu.add(viewMenu);
JMenuItem kx = new JMenuItem("科学型");
viewMenu.add(kx);
JMenuItem bz = new JMenuItem("标准型");
viewMenu.add(bz);
JMenu bzcd = new JMenu("帮助");
editMenu.add(bzcd);
JMenuItem bangzu = new JMenuItem("帮助");
bzcd.add(bangzu);
JMenuItem gy = new JMenuItem("关于");
bzcd.add(gy);
Button btnEast = new Button("don");
Button btnWest = new Button("don");
Button btnNorth = new Button("don");
Button btnSouth = new Button("don");
Button btnCenter = new Button("don");
jframe.add(btnEast, BorderLayout.EAST);
jframe.add(btnWest, BorderLayout.WEST);
jframe.add(btnNorth, BorderLayout.NORTH);
jframe. add(btnSouth, BorderLayout.SOUTH);
jframe. add(btnCenter, BorderLayout.CENTER);
jframe.setVisible(true);
}
public static void main(String[] args) {
SimpleCalcuatorFrame sc = new SimpleCalcuatorFrame();
}
}
或者
import java.awt.*;
import javax.swing.*;
public class SimpleCalcuatorFrame extends JFrame {
public SimpleCalcuatorFrame() {
setTitle("简易计算器 v1.0");
setSize(400, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setResizable(false);
JMenuBar editMenu = new JMenuBar();
setJMenuBar(editMenu);
JMenu jmenu = new JMenu("编辑");
editMenu.add(jmenu);
JMenuItem copyMenuItem = new JMenuItem("复制");
jmenu.add(copyMenuItem);
JSeparator s = new JSeparator();
jmenu.add(s);
JMenuItem pMenu = new JMenuItem("粘贴");
jmenu.add(pMenu);
JMenu viewMenu = new JMenu("查看");
editMenu.add(viewMenu);
JMenuItem kx = new JMenuItem("科学型");
viewMenu.add(kx);
JMenuItem bz = new JMenuItem("标准型");
viewMenu.add(bz);
JMenu bzcd = new JMenu("帮助");
editMenu.add(bzcd);
JMenuItem bangzu = new JMenuItem("帮助");
bzcd.add(bangzu);
JMenuItem gy = new JMenuItem("关于");
bzcd.add(gy);
Button btnEast = new Button("don");
Button btnWest = new Button("don");
Button btnNorth = new Button("don");
Button btnSouth = new Button("don");
Button btnCenter = new Button("don");
add(btnEast, BorderLayout.EAST);
add(btnWest, BorderLayout.WEST);
add(btnNorth, BorderLayout.NORTH);
add(btnSouth, BorderLayout.SOUTH);
add(btnCenter, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
SimpleCalcuatorFrame sc = new SimpleCalcuatorFrame();
}
}
效果图
更多追问追答
追问
还有就是在布局管理器中可以添加容器吗?就比如在边界布局的WEST那一块可以嵌入一个容器吗?
追答
布局可以嵌套布局
比如WEST 的地方,不要放一个JButton
放入一个JPanel ,然后给JPanel 里面添加 JButton 等,可以放多个组件
因为JPanel默认是流式布局
展开全部
这么改就好了
public class SimpleCalcuatorFrame extends JFrame
{
public SimpleCalcuatorFrame()
{
//JFrame jframe = new JFrame("简易计算器 v1.0");
this.setSize(400, 250);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setResizable(false);
JMenuBar editMenu = new JMenuBar();
this.setJMenuBar(editMenu);
JMenu jmenu = new JMenu("编辑");
editMenu.add(jmenu);
JMenuItem copyMenuItem = new JMenuItem("复制");
jmenu.add(copyMenuItem);
JSeparator s = new JSeparator();
jmenu.add(s);
JMenuItem pMenu = new JMenuItem("粘贴");
jmenu.add(pMenu);
JMenu viewMenu = new JMenu("查看");
editMenu.add(viewMenu);
JMenuItem kx = new JMenuItem("科学型");
viewMenu.add(kx);
JMenuItem bz = new JMenuItem("标准型");
viewMenu.add(bz);
JMenu bzcd = new JMenu("帮助");
editMenu.add(bzcd);
JMenuItem bangzu = new JMenuItem("帮助");
bzcd.add(bangzu);
JMenuItem gy = new JMenuItem("关于");
bzcd.add(gy);
BorderLayout borderlayout = new BorderLayout();
setLayout(borderlayout);
Button btnEast = new Button("don");
Button btnWest = new Button("don");
Button btnNorth = new Button("don");
Button btnSouth = new Button("don");
Button btnCenter = new Button("don");
add(btnEast, BorderLayout.EAST);
add(btnWest, BorderLayout.WEST);
add(btnNorth, BorderLayout.NORTH);
add(btnSouth, BorderLayout.SOUTH);
add(btnCenter, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args)
{
SimpleCalcuatorFrame sc = new SimpleCalcuatorFrame();
}
}
不要在构造方法里new一个窗体对象,而是在当前窗体对象上set布局和控件
public class SimpleCalcuatorFrame extends JFrame
{
public SimpleCalcuatorFrame()
{
//JFrame jframe = new JFrame("简易计算器 v1.0");
this.setSize(400, 250);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setResizable(false);
JMenuBar editMenu = new JMenuBar();
this.setJMenuBar(editMenu);
JMenu jmenu = new JMenu("编辑");
editMenu.add(jmenu);
JMenuItem copyMenuItem = new JMenuItem("复制");
jmenu.add(copyMenuItem);
JSeparator s = new JSeparator();
jmenu.add(s);
JMenuItem pMenu = new JMenuItem("粘贴");
jmenu.add(pMenu);
JMenu viewMenu = new JMenu("查看");
editMenu.add(viewMenu);
JMenuItem kx = new JMenuItem("科学型");
viewMenu.add(kx);
JMenuItem bz = new JMenuItem("标准型");
viewMenu.add(bz);
JMenu bzcd = new JMenu("帮助");
editMenu.add(bzcd);
JMenuItem bangzu = new JMenuItem("帮助");
bzcd.add(bangzu);
JMenuItem gy = new JMenuItem("关于");
bzcd.add(gy);
BorderLayout borderlayout = new BorderLayout();
setLayout(borderlayout);
Button btnEast = new Button("don");
Button btnWest = new Button("don");
Button btnNorth = new Button("don");
Button btnSouth = new Button("don");
Button btnCenter = new Button("don");
add(btnEast, BorderLayout.EAST);
add(btnWest, BorderLayout.WEST);
add(btnNorth, BorderLayout.NORTH);
add(btnSouth, BorderLayout.SOUTH);
add(btnCenter, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args)
{
SimpleCalcuatorFrame sc = new SimpleCalcuatorFrame();
}
}
不要在构造方法里new一个窗体对象,而是在当前窗体对象上set布局和控件
追问
您好,怎么设置文本框的宽度自适应窗体大小,就是说无论怎么拉伸窗体,文本框都是跟窗体的变化而发生改变的?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询