JFrame里怎么添加一个JMenu和JPanel

我想在一个面板里添加一个菜单栏 然后点击里面的菜单项时 下面可以显示做好的class 这是我写的 可是显示不出来

public MainFrame()
{
this.setTitle("MainFrame");
container = this.getContentPane();
container.setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
//buildMainMenu(menuBar);

//protected void buildMainMenu(JMenuBar menuBar)

    JMenu filemenu = new JMenu("文件");
    JMenuItem exit  = new JMenuItem("退出");
    exit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            setVisible(false);
            dispose();
            System.exit(0);
        }
    });

    filemenu.add(exit);
    menuBar.add(filemenu);
    /*演示程序查询菜单*/
    JMenu demoMenu = new JMenu("信息管理");
    item1 = new JMenuItem("增加信息");
    item2 = new JMenuItem("删除信息");
    item3 = new JMenuItem("查找信息");
    item4 = new JMenuItem("修改信息");
    item1.addActionListener(new ItemsActionListener());
    item2.addActionListener(new ItemsActionListener());
    item3.addActionListener(new ItemsActionListener());
    item4.addActionListener(new ItemsActionListener());
    menuBar.add(demoMenu);
    demoMenu.add(item1);
    demoMenu.add(item2);
    demoMenu.add(item3);
    demoMenu.add(item4);
    JMenu helpMenu = new JMenu("帮助");
    JMenuItem aboutMenuItem =  new JMenuItem("关于");
    aboutMenuItem.addActionListener(new AboutActionListener());
    helpMenu.add(aboutMenuItem);
    menuBar.add(helpMenu);
    p1= new JPanel();
    p1.setBackground(Color.BLUE);
    p1.setSize(100,300);
    p1.add(button1);
    p1.setVisible(true);
    container.add(p1);

    /*menubar.add(item1);
    menubar.add(item2);
    menubar.add(item3);*/
    this.setJMenuBar(menuBar);//构造菜单条
    this.setVisible(true);
    this.setSize(600,450);
    JFrame jf = new JFrame();
    jf.add(menuBar);
    jf.add(p1);
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
怎么在JPanel里添加图片?
怎么不行啊? 在JBuilder里做的...
JPanel JFrame
创建了JFrame application=new JFrame();rn又创建了多个JPanel 对象画出不同的图,怎样将这多个JPanel对象加到application 上能显示所有图,调用application.add会覆盖,求实现方法,最好有代码,谢谢
多个JPanel在一个JFrame上。JPanel之间的距离怎么控制?
菜鸟问!多个JPanel在一个JFrame上。JPanel之间的距离怎么控制?每个JPanel 的边框怎么画出来?程序运行后想看到 5个不同 JPanel之间的明显的隔离带,也就是边线!谢谢大侠啦!rnrnrnimport com.borland.jbcl.layout.*;rnimport java.awt.*;rnimport javax.swing.*;rnrnpublic class MainFrame extends JFrame rn XYLayout xYLayout1 = new XYLayout();rn JPanel jPanel1 = new JPanel();rn JPanel jPanel2 = new JPanel();rn JButton jButton1 = new JButton();rn JTextField jTextField1 = new JTextField();rn JPanel jPanel3 = new JPanel();rn JPanel jPanel4 = new JPanel();rn JPanel jPanel5 = new JPanel();rn JButton jButton2 = new JButton();rn JButton jButton3 = new JButton();rn JButton jButton4 = new JButton();rn public MainFrame() rn try rn jbInit();rn rn catch(Exception e) rn e.printStackTrace();rn rn rn private void jbInit() throws Exception rn this.getContentPane().setLayout(xYLayout1);rn jButton1.setBounds(new Rectangle(42, 54, 71, 23));rn jButton1.setText("jButton1");rn jPanel1.setLayout(null);rn jPanel2.setLayout(null);rn jTextField1.setText("jTextField1");rn jTextField1.setBounds(new Rectangle(45, 86, 62, 20));rn jPanel3.setLayout(null);rn jPanel4.setLayout(null);rn jPanel5.setLayout(null);rn jButton2.setBounds(new Rectangle(42, 26, 71, 23));rn jButton2.setText("jButton2");rn jButton3.setBounds(new Rectangle(59, 36, 71, 23));rn jButton3.setText("jButton3");rn jButton4.setBounds(new Rectangle(63, 28, 71, 23));rn jButton4.setText("jButton4");rn xYLayout1.setWidth(480);rn xYLayout1.setHeight(339);rn this.getContentPane().add(jPanel1, new XYConstraints(5, 2, 153, 128));rn jPanel1.add(jButton1, null);rn this.getContentPane().add(jPanel2, new XYConstraints(7, 141, 187, 147));rn jPanel2.add(jTextField1, null);rn this.getContentPane().add(jPanel3, new XYConstraints(243, 22, 147, 72));rn jPanel3.add(jButton2, null);rn this.getContentPane().add(jPanel4, new XYConstraints(238, 108, 159, 84));rn jPanel4.add(jButton3, null);rn this.getContentPane().add(jPanel5, new XYConstraints(234, 203, 165, 87));rn jPanel5.add(jButton4, null);rn rn rnrn下面的是大侠老紫竹的上个帖子的提示·············································································rnrn你把jbuilder/lib 下面的 jbcl.jar 导入到 eclipse的build path里面就可以了!rnrn如果不知道怎么添加自己的类库,请参考这个rnhttp://www.java2000.net/viewthread.jsp?tid=463rn如果找不到,也可以到这里下载,点那个downloadrnhttp://cougaar.org/cgi-bin/viewcvs.cgi/CougaarUnit/lib/jbcl.jar?cvsroot=cougaarunit·老紫竹 rn www.java2000.net·rn.........................................................................................................
一个关于JFrame和JPanel的问题
import java.awt.*;rnimport javax.swing.*;rnimport java.awt.event.*;rnrnpublic class Calculatorrnrn public static void main(String[] args)rn rn JFrame frame=new JFrame("Calculator");rn frame.addWindowListener(new WindowAdapter()rn rn public void windowClosing(WindowEvent e)rn rn System.exit(0);rn rn );rnrn Container c=frame.getContentPane();rn JPanel pane1=new JPanel();rn pane1.setLayout(new GridLayout(4,6,3,3));rn pane1.add(new JButton("sin"));rn pane1.add(new JButton("MC"));rn pane1.add(new JButton("0"));rn pane1.add(new JButton("1"));rn pane1.add(new JButton("2"));rn pane1.add(new JButton("+"));rn pane1.add(new JButton("cos"));rn pane1.add(new JButton("MR"));rn pane1.add(new JButton("3"));rn pane1.add(new JButton("4"));rn pane1.add(new JButton("5"));rn pane1.add(new JButton("-"));rn pane1.add(new JButton("tan"));rn pane1.add(new JButton("MS"));rn pane1.add(new JButton("6"));rn pane1.add(new JButton("7"));rn pane1.add(new JButton("8"));rn pane1.add(new JButton("*"));rn pane1.add(new JButton("+/-"));rn pane1.add(new JButton("M+"));rn pane1.add(new JButton("9"));rn pane1.add(new JButton("."));rn pane1.add(new JButton("="));rn pane1.add(new JButton("/"));rnrn JPanel pane2=new JPanel();rn pane2.setLayout(null);rnrn JTextField tf1=new JTextField();rn tf1.setBounds(0,0,30,20);rn tf1.setEditable(false);rn tf1.setBackground(Color.blue);rn pane2.add(tf1);rnrn c.add(BorderLayout.CENTER,pane1);rn c.add(BorderLayout.NORTH,pane2);rn frame.setSize(350,200);rn frame.setVisible(true);rn rnrn为什么pane2显示不出来呢?
在按键事件中向JFrame添加JPanel问题
rnrn 大家好,请大家帮个忙,我通过JFrame上的按钮事件向JFrame添加JPanel失败,代码大致如下:rn rn public class mainFrame rn rn public mainFrame()rn rn ...rnrn linkButton.addMouseListener(new MouseAdapter()rn rn public void mouseClicked(MouseEvent e)rn rn showMessage();rn // System.out.println("OK");rn rn ); rnrn rnrn public int showMessage()rn rn .....rn mainPanel pen = new mainPanel(0);rn pen.setBounds(100, 50, 50, 50);rn mainFrame.add(pen); rn rnrnrnmainPanel(),通过不同的参数加载不同的图片rn如果把添加panel 放到构造函数中可以显示出来,但放到事件中显示不出来,请大家帮帮忙rnrn
往JFrame里面添加Jpanel出现java.lang.ClassCastException
我新建了一个类rnpublic class Pro_BuildingInfo extends JPanel implements ActionListenerrn现在想通过rnJPanel jp = new Pro_BuildingInfo();rnadd(jp);rn将Pro_BuildingInfo 加入到新建的Menu主界面里,结果出现了rn[img=https://img-bbs.csdn.net/upload/201403/10/1394427983_156141.png][/img]rn而且没有可以跳转到我写的类里面的错误链接,求大神科普怎么回事啊
怎么给JMenu添加图片
偶然看到一个小程序。。JMenu里面有图片。。。rn请问这是怎么实现的。rn我觉得不是背景,图片后面跟着文字。。。rn小弟刚学Java不久。。。请大侠指点下~~~
如何在JFrame或JPanel里添加背景图片
我的问题在标题里已经说得很清楚了,有谁知道?
Jpanel或Jframe添加键盘监听无效
无法实现键盘监是因为没有获取屏幕焦点,将需要监听的控件获取屏幕焦点即可,代码如下: 控件名.setFocusable(true); /*注意要写setVisible(true)后面才行。*/
JFrame对象中添加JPanel容器的困惑
一个JFrame容器中究竟可以加几个JPanel容器?有的书上说是可以加好多个的,可是小弟昨天发现如果直接添加JPanel容器到JFrame容器中的话,就会发生容器覆盖的问题。请问这究竟是怎么回事?还有啊,关于这两个容器的区别我也不是很清楚。rn前几天听人说,JFrame容器是顶级容器,JPanel不是,但是JFrame容器不能直接添加大部分组件,必须借助2级容器JPanel的帮助。但是小弟今天又在书上看到这样的代码:rnrnrnrnimport javax.swing.JFrame;rnimport javax.swing.JButton;rnrnrnpublic class JframernrnJframe()rn JFrame jf=new JFrame("my frame");rn JButton b=new JButton("my button1" );rn rn jf.add(b);rn rn jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rn jf.setVisible(true);rnrnrnpublic static void main(String[] args)rnrnnew Jframe();rnrnrnrnrn请问,JFrame容器到底可以直接加JButton组件吗?还有,如果我想在一个JFrame容器内加两个JPanel容器不让他们相互覆盖,该怎样做呢?小弟在线等大神回答rnrnrn
JFrame监听JPanel里的clicked事件
现在有个JFrame 叫mainFrame,有个自定义的JPanel 叫numPanel,在mainFrame的特定位置会通过构造函数new一个numPanel,这样,numPanel就被mainFrame包含在里面了。rnnumPanel里面有个按钮,点击它,触发了对应的clicked事件。现在在JFrame里面有个函数,只要监听到numPanel里面的那个clicked事件触发,就会执行。应该怎样实现这个监听呢?
JFrame和JPanel里的布局为什么不一样呢?
从JBuilder 里找了个例子,在原来的基础上又回了个JPanel,界面就变了呢?rn这是我改后的代码,大家看看这是什么问题。rnimport java.awt.*;rnimport java.awt.event.*;rnimport javax.swing.*;rnimport javax.swing.border.*;rnimport java.awt.Rectangle;rnrnpublic class Frame1 extends JFrame rnrn //UI and componentsrn JPanel contentPane;rn JPanel panel = new JPanel();rn JMenuBar menuBar1 = new JMenuBar();rn JMenu menuFile = new JMenu();rn JMenuItem menuFileExit = new JMenuItem();rn TitledBorder titledBorder1;rn JPasswordField jPasswordField1 = new JPasswordField(10);rn JTextField jTextField1 = new JTextField();rn JLabel jLabel1 = new JLabel();rn JLabel jLabel2 = new JLabel();rn JButton jButton1 = new JButton();rn JOptionPane jOptionPane1 = new JOptionPane(); /**rn * Construct the framern */rn public Frame1() rn enableEvents(AWTEvent.WINDOW_EVENT_MASK);rn try rn jbInit();rn rn catch(Exception e) rn e.printStackTrace();rn rn rnrn /**rn * Component initializationrn *rn * @throws Exception exceptionrn */rn private void jbInit() throws Exception rnrn //initialized content panern contentPane = (JPanel) this.getContentPane();rn titledBorder1 = new TitledBorder("");rn contentPane.setLayout(new BorderLayout());rn this.setSize(new Dimension(350, 300));rn this.setTitle("Password Sample");rnrn //initialized menu bar and menu items, as well as menu event listenersrn this.setJMenuBar(menuBar1);rn menuFile.setText("File");rn menuFileExit.setText("Exit");rn menuFileExit.addActionListener(new ActionListener() rn public void actionPerformed(ActionEvent e) rn fileExit_actionPerformed(e);rn rn );rnrn //initialize password field - set echo character to '#' and lengthrn //of field to 10 columns. Add event listenerrn jPasswordField1.setEchoChar('#');rn jPasswordField1.setColumns(10);rn jPasswordField1.setBounds(new Rectangle(107, 77, 109, 28));rn jPasswordField1.addActionListener(new java.awt.event.ActionListener() rn public void actionPerformed(ActionEvent e) rn jPasswordField1_actionPerformed(e);rn rn );rnrn //initialize text field for messagern jTextField1.setBackground(Color.red);rn jTextField1.setFont(new java.awt.Font("SansSerif", 1, 24));rn jTextField1.setBorder(BorderFactory.createRaisedBevelBorder());rn jTextField1.setCaretColor(Color.white);rn jTextField1.setEditable(false);rn jTextField1.setHorizontalAlignment(SwingConstants.CENTER);rn jTextField1.setBounds(new Rectangle(6, 145, 329, 101));rnrn //initialize labelsrn jLabel1.setText("Password is:");rn jLabel1.setBounds(new Rectangle(5, 33, 109, 29));rn jLabel2.setText("Enter password:");rn jLabel2.setBounds(new Rectangle(6, 76, 99, 28));rnrn //initialize button and create event listenerrn jButton1.setFont(new java.awt.Font("SansSerif", 0, 12));rn jButton1.setBorder(BorderFactory.createRaisedBevelBorder());rn jButton1.setText("Click for password");rn jButton1.setBounds(new Rectangle(106, 22, 142, 44));rn jButton1.addActionListener(new java.awt.event.ActionListener() rn public void actionPerformed(ActionEvent e) rn jButton1_actionPerformed(e);rn rn ); //add componentsrn menuFile.add(menuFileExit);rn menuBar1.add(menuFile);rn panel.add(jTextField1, null);rn panel.add(jLabel1, null);rn panel.add(jLabel2, null);rn panel.add(jPasswordField1, null);rn panel.add(jButton1, null);rn contentPane.add(panel,BorderLayout.CENTER);rn rnrn /**rn * File | Exit action performedrn *rn * @param e ActionEventrn */rn public void fileExit_actionPerformed(ActionEvent e) rn System.exit(0);rn rnrn /**rn * Overridden so we can exit when window is closedrn *rn * @param e WindowEventrn */rn protected void processWindowEvent(WindowEvent e) rn super.processWindowEvent(e);rn if (e.getID() == WindowEvent.WINDOW_CLOSING) rn fileExit_actionPerformed(null);rn rn rnrnrn private boolean isPasswordCorrect(char[] input) rn char[] correctPassword = 'j', 'b', 'u', 'i', 'l', 'd', 'e', 'r' ;rn if (input.length != correctPassword.length)rn return false;rn for (int i = 0; i < input.length; i ++)rn if (input[i] != correctPassword[i])rn return false;rn return true;rn rnrnrn void jPasswordField1_actionPerformed(ActionEvent e) rn String correctPassword = new String("Good job!");rn String incorrectPassword = new String("Opps! Try again!");rnrn JPasswordField input = (JPasswordField)e.getSource();rn char[] password = input.getPassword();rn if (isPasswordCorrect(password)) rn jTextField1.setText(correctPassword);rn JOptionPane.showMessageDialog(jOptionPane1, "Success! You typed the right password.");rn rn else rn jTextField1.setText(incorrectPassword);rn JOptionPane.showMessageDialog(jOptionPane1, "Invalid password. Try again.",rn "Error Message", JOptionPane.ERROR_MESSAGE);rn rn rnrnrn void jButton1_actionPerformed(ActionEvent e) rn jButton1.setText("jbuilder");rn rn
请教JFrame 和 JPanel 的关系?
谢谢
JFrame里面的JPanel能重叠吗?
比如说 在一个JFrame里面有4个JPanel,一个用来做背景,一个用来做人物动画rn一个用来放按钮组件等,能不能设置为透明叠加?用什么来控制它在那层的优先级?
JFrame里的JPanel切换成第二个JPane
创建一个包rn在该包中新建一个类,从JFrame继承下来,在该JFrame里边建一个JPanelrn另建一个类,从JPanel继承下来rn如何将当前JFrame里的JPanel切换成第二个JPanern(表述不是太清楚)rn也就是,新建一个JPanel把当前JFrame中的JPanel替换掉
有个问题,我的一个JFrame里包括一个JPanel
然户我想JPanel的一个ok JButton单击rn弹出一个JDialog,我还想调用JPanel的一些属性,rn我不知道该怎么引用这个JPanel,因为JDialog的构造函数rn要求传递的是JFrame,不能是JPanel。rn难道只能把这个JPanel当作其他参数传递给JDialog?rnrn另外,我的JPanel是写在一个单独的类文件中,我怎么去引用rn包括他的JFrame呢?有没有办法可以得到一个组件的父组件是什么?rnrn
JPanel,JFrame 设置背景图片???
JPanel和JFrame 中能不能加载背景图片?rnJButton,JLabel 上能不能只设置图片,不要文字?rn最好给出具体代码,谢谢拉!!
JFrame JPanel 调用问题
我坐了一个菜单类:Menu.javarnrn一个主界面类:Manager.javarnrn两个个显示界面类:Index.java、Select.javarnrn开始我显示的是:Index.javarnrn然后我想通过菜单调用Select.java ,并把Index.java去掉,就是替换咯。rnrn所以我监听菜单类,想了很久没有结果想请给位帮下拉!!rnrnrn下面是[color=#FF0000]主界面[/color]类:Manager.java的一段主要代码,意思是网格布局将new Menu().jmb的菜单条添加到第一格,将new Index()第一次显示至第二格,之后问题来了,我要怎么监听菜单,触发使得new Select()添加到主界面里去。rnrnthis.setLayout(new GridLayout(1,1));rnthis.setJMenuBar(new Menu().jmb); rnthis.getContentPane().add(new Index());rnrnrn-----------下面是菜单类代码---rn[code=Java]public class Menu extends JPanel implements ActionListener rnrn JMenuBar jmb;rn JMenu jm;rn JMenu jm1S;rn JMenuItem jm1S1;rnrn public Menu() rn rnrn jmb = new JMenuBar(); // 菜单条rn jm = new JMenu("信息"); // 菜单rn rn jm1S = new JMenu("查询"); rn jm1S1 = new JMenuItem("按姓名查询");rn jm1S1.addActionListener(this);rn rn jmb.add(jm);rn jm.add(jm1S);rn jm1S.add(jm1S1);rnrn rn rn rnrn @Overridern public void actionPerformed(ActionEvent e) rn // TODO Auto-generated method stubrn if (e.getSource() == jm1S1) rnrn rnrn rn rnrn[/code]rn
JPanel和JFrame的尺寸的问题
1.tutorial说JFrame是top-level的container,JPanel也是containerrn那么一个JPanel的是不是充满包含它的那个JFrame?rn也就是说,JFrame多大,它包含的那个JPanel就多大?rnrn2.指定component坐标时,直接用setLocation()方法就可以了吗?rn它的参数是不是相对于JPanel的相对坐标?rn
JPanel和JFrame的区别是什么?
各适用于什么情况???
关于JFrame和JPanel的问题
我的JFrame里面定义了两个JPanelrn一个是一个由一个线程一直重画的,一个就是静静的呆那的,刚开始的时候不创建这两个JPanelrn在点击菜单栏的里面的事件里创建,并设置了布局,问题是那个一直重画的JPanel出来了,而那个呆那的没出来rn我以为是JPanel重画的原因,所以在创建并加入那个呆那的JPanel后我就手动的repaint()了一下,但是还是一样的没效果(JFrame重绘也是试过,一样)rnrn更奇怪的是,在我放大了窗口,那个一直重画的JPanel反而不见了,那个呆那的出来了...缩回去也没用...超郁闷rnrn还有如果设了JFrame的布局为null的话,那个呆那的JPanel就一直出不来,那个一直重画的出来了...----对了,那个一直呆那的JPanel是用netbeans的可视化界面做的,那个一直重画的是手写的---难道是用可视化的问题?rn这问题弄的一晚上没弄好,各位帮帮忙啊~~先谢过
JFrame里动态显示其它JFrame或JPanel该如何实现?
就是根据不同菜单的点击在主窗体里(含菜单的)的某一区域显示不同的JFrame或JPanel,请问该如何实现?rn还有,JFrame的话怎样才能不显示它的标题栏,谢谢。
请问,这样在一个jframe中动态改变(添加)jpanel?
我想当用户在菜单中选择以后,不是出现一个对话框或者一个新的frame,而是直接出现在本frame中(我不想用JTabbedPane),能告诉我该怎么解决吗?哪位大牛帮帮忙啊,急啊!!
JFrame添加一个JPanel,怎么会畸形成这个样子?
各位大神,我是在一个JFrame中添加了一个自定义的Panel,怎么会变成xia mi a mrnrn[code=java]rnimport javax.swing.JFrame;rnrnpublic class MainClass rnrn public static void main(String[] args) rn // TODO Auto-generated method stubrn rn JFrame jf = new JFrame("模式管理工具");rn jf.setSize(600, 700);rn jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rn jf.setLocationRelativeTo(null);rn jf.setLayout(null);rn jf.setVisible(true); rnrn ESConnect_UI e = new ESConnect_UI();rn e.setBounds(0, 0, 600, 200);rn jf.getContentPane().add(e);rn rnrn[/code]rnrnrnrn[code=java]rnimport java.awt.Button;rnimport java.awt.Color;rnimport java.awt.TextField;rnrnimport javax.swing.JLabel;rnimport javax.swing.JPanel;rnrnpublic class ESConnect_UI extends JPanelrn JLabel tip = new JLabel("连接信息");rn JLabel l_ip = new JLabel("IP地址");rn JLabel l_port = new JLabel("端口");rn TextField ip = new TextField();rn TextField port = new TextField();rn Button test = new Button("测试连接");rn rn ESConnect_UI()rn rn this.setLayout(null);rn this.setSize(600, 200);rn ip.setText("127.0.0.1");rn port.setText("12345");rn rn tip.setBounds(40, 40, 120, 20);rn l_ip.setBounds(40, 70, 100, 20);rn l_port.setBounds(40, 100, 100, 20);rn ip.setBounds(80, 70, 100, 20);rn port.setBounds(150, 100, 100, 20);rn test.setBounds(150, 160, 60, 20);rn rn this.add(tip);rn this.add(l_ip);rn this.add(l_port);rn this.add(ip);rn this.add(port);rn this.add(test);rn this.setBackground(Color.BLUE);rn System.out.println(this.getAlignmentX()+" "+this.getAlignmentY());rn System.out.println(this.getHeight()+" "+this.getWidth());rn rn rnrn[/code]rnrn[img=https://img-bbs.csdn.net/upload/201512/07/1449500994_131508.png][/img]rnrn
JRadioButton在JPanel,JPanel在JFrame上面,怎么捕捉事件
写了一个代码,一个JFrame( 称为 A )上面安装了一个 BorderLayout, 上面有4个JPanel,在其中的一个JPanel(称为 B)上面又有一个JPanel (称为 C ), 我在C上面实现了 ActionListener 的 actionPerformed() 但是好像捉不到消息,大家帮我看看是怎么会回事啊,我把相关的代买贴上来了rnrn谢谢各位了rnrnrn[code=Java]rn// JPanle C 上面的rnrnpublic class VirtualWorldManagerSettingPanel extends JPanel implementsrn ActionListener rnrn JLabel statue = new JLabel("off-line");rn rn JRadioButton VisSimulation = new JRadioButton("Vis Simulation");rn JRadioButton GenSimulation = new JRadioButton("Gen Simulation");rnrn ButtonGroup group = new ButtonGroup();rn rn public VirtualWorldManagerSettingPanel() rnrn this.setBorder(BorderFactoryrn .createTitledBorder("Virtual World Manager"));rnrnrn // this.setSize(new Dimension(400,200));rn rn group.add(VisSimulation);rn group.add(GenSimulation);rn rn JPanel jp = new JPanel();rn jp.setLayout(new GridLayout(2, 0));rn rn jp.add(VisSimulation);rn jp.add(GenSimulation);rn rn GenSimulation.setSelected(true);rn rn this.add(jp);rnrn rnrn @Overridern public void actionPerformed(ActionEvent arg0) rn // TODO Auto-generated method stubrn if( arg0.getSource().equals("Vis Simulation"))rnSystem.out.println("Vis Simulation"); rn rn rn if( arg0.getSource().equals("Gen Simulation"))rnSystem.out.println("Vis Simulation"); rn rn rnrnrnrn[/code]rnrnrn[code=Java]rn// JPanle B 和 JFrame 都写在一起了rnrnpublic class MASSimulator extends JFrame rnrn private static MASSimulator INSTANCE;rnrn // panels that are the basical layout of main framern private JPanel North;rnrn // the panels thatrnrn private VirtualWorldManagerSettingPanel vwmsp = new VirtualWorldManagerSettingPanel();rnrnrnrn private MASSimulator() rnrn this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rnrn rnrn rn private void buildInterface() rnrn this.buildNorthPanel();rnrnrn rnrn private void buildNorthPanel() rn this.North = new JPanel();rnrnrn this.North.add(this.vwmsp);rnrn this.add(this.North, BorderLayout.NORTH);rn rnrn rn @Overridern protected void processWindowEvent(WindowEvent e) rnrn if (e.getID() == WindowEvent.WINDOW_CLOSING) rnrn Calendar cal = Calendar.getInstance();rn String name = cal.getTime().getHours() + "_"rn + cal.getTime().getMinutes() + "_"rn + cal.getTime().getSeconds();rnrn ProcessLogStorage.getInstance().saveSimulationLogAsXML(rn "./Logs/" + name + ".mxml");rn rnrn super.processWindowEvent(e);rnrn rnrn rnrn public static MASSimulator getInstance() rn if (INSTANCE == null) rn INSTANCE = new MASSimulator();rn rn return INSTANCE;rn rnrn public static void main(String[] arg) rn getInstance().setVisible(true);rn rnrnrnrn[/code]rnrnrn
一个JFrame程序怎么修改,添加到一个程序的JPanel中?
下载了一个程序模板,其中右下角有自定义的JPanel部分。rnrn另有一个JFrame的摄像头调用程序,请问怎么修改可添加到以上JPanel中去?rnrn我改了几次,都无法正确显示摄像头,而且原模板结构也被破坏了。rnrn希望大家说说思路和需要注意的地方,我再试试。
在一个JPanel如何获取主界面JFrame其他JPanel的信息
我在一个JFrame里面加了多个JPanel,每个JPanel都用不同的类编写的。rn我想在其中一个JPanel(这里叫做jPanel1)里面添加一个按钮监听来控制另外一个JPanel(比如jPanel2)里面文本框显示的内容。rn请问是否能只在jPanel类里面做相应的操作就可以获取jPanel2,并对其进行修改。rn
如何"缝制"一个JPanel到JFrame上面
public class MyPanel extends JPanel { ImageIcon icon; Image img; public MyPanel() { icon = new ImageIcon("图片的路径"); img = icon.getImage(); } public void paintCompo
一个JPanel跟JFrame错位的问题,怎么办?
是这样的,弄了个登录的窗口,外面有JFrame 其上有个JPanel, JPanel有张图片,有用户名、密码等文本域和两个按钮,rn当我把JFrame跟JPanel的大小设成一样后,显示不出panel上的文本域 背景图片等rn当我把JFrame的大小改到很大后却可以看到那些组件都在靠近中间的位置,可是,真正能够输入文本信息的却是在左上角...rn如图:rn[img=http://hiphotos.baidu.com/moneysure/pic/item/5720d10328908ab0d53f7c8f.jpg][/img]rnrn虽然显示出来了,但真正的文本域等其实是在左上角画红线处。郁闷...rn下面是源代码,哪位大虾能帮帮我吗? 感激不尽!rn[code=Java]public class LoginFrame extends JFrame rn final BackP jBPanel = new BackP(260, 170);rn /**构造登录窗体rn * Create the frame.rn */rn public LoginFrame() rn setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);rn setResizable(false); // 不允许改变大小 rn getContentPane().add(jBPanel, java.awt.BorderLayout.CENTER);rn pack();rn setTitle("登录系统");rn setSize(650,670); // 窗体大小rn Toolkit toolkit = getToolkit(); // 获得Toolkit对象rn Dimension dimension = toolkit.getScreenSize(); // 获得Dimension对象rn int screenHeight = dimension.height; // 获得屏幕的高度rn int screenWidth = dimension.width; // 获得屏幕的宽度rn int frm_Height = this.getHeight(); // 获得窗体的高度rn int frm_width = this.getWidth(); // 获得窗体的宽度rn this.setLocation((screenWidth - frm_width) / 2,rn (screenHeight - frm_Height) / 2); // 使用窗体居中显示rn setAlwaysOnTop(true);rn rnrn /**rn * 隐藏系统登录窗体rn */rn private void hideLoginFrame()rn this.setVisible(false);rn rn rn /**rn * 背景面板类rn */rn private class BackP extends JPanelrn rn private JButton btn_login;rn private JButton btn_exit;rn private JTextField text_user;rn private JPasswordField text_pwd;rn private JLabel l_user;rn private JLabel l_pwd; rn Image image;rn int x,y;rn /**rn * 面板构造方法rn * @param x 宽度rn * @param y 高度rn */rn public BackP(int x,int y)rn rn URL url = BackP.class.getResource("/res/login.jpg"); // 获得图片的URL rn image = new ImageIcon(url).getImage(); // 创建图象对象rn setLayout(null); rn add(getl_user());rn add(getl_pwd());rn add(gettext_user());rn add(gettext_pwd());rn add(getBtn_enter());rn add(getBtn_exit()); rn this.x=x; rn this.y=y;rn rn rn /**rn * 设置图象宽度rn * @param x 宽度rn */rn public void setX(int x) rn this.x = x;rn rn rn /**rn * 获得图象宽度rn * @returnrn */rn public int getX() rn return x;rn rn rn /**rn * 设置图象高度rn * @param yrn */rn public void setY(int y) rn this.y=y;rn rn rn /**rn * 获得图象高度rn * @returnrn */rn public int getY() rn return y;rn rn rn /**rn * 重写paintComponent方法rn * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)rn */rn protected void paintComponent(Graphics g) rn super.paintComponent(g); // 调用父类的方法rn Graphics2D g2 = (Graphics2D) g; // 创建Graphics2D对象rn g2.drawImage(image, 0, 0, x, y, this); // 绘制图象rn rn rn rn /**rn * rn */rn /*rn protected JComboBox getc_type()rn rn String[] typ="前台护士","医生","药房人员","管理员";rn if(c_type==null)rn rn c_type=new JComboBox();rn c_type.setName("c_type");rn c_type.setSelectedItem(typ);rn c_type.setBounds(74,80,50,25);rn rn return c_type;rn rn rn protected JLabel getl_type()rn rn if (l_type==null) rn rn l_type= new JLabel();rn l_type.setText("身份:");rn l_type.setBounds(24, 80, 50, 20);rn rn return l_type;rn rn */rn /**rn * @returnrn */rn protected JLabel getl_user()rn rn if (l_user==null) rn rn l_user= new JLabel();rn l_user.setText("用户名:");rn l_user.setBounds(24, 10, 50, 20);rn rn return l_user;rn rn /**rn * @returnrn */rn protected JLabel getl_pwd() rn rn if (l_pwd== null) rn rn l_pwd= new JLabel();rn l_pwd.setText("密码:");rn l_pwd.setBounds(24, 50, 50, 20);rn rn return l_pwd;rn rn /**rn * @returnrn */rn protected JTextField gettext_user()rn rn if (text_user==null) rn rn text_user=new JTextField();rn text_user.addActionListener(new ActionListener() rn public void actionPerformed(final ActionEvent evt) rn text_userActionPerformed(evt); // 调用方法响应事件操作rn rn );rn text_user.setName("text_user");rn text_user.setBounds(74, 10, 150, 25);rn rn return text_user;rn rn /**rn * @returnrn */rn protected JPasswordField gettext_pwd() rn rn if (text_pwd==null) rn text_pwd=new JPasswordField();rn text_pwd.addActionListener(new ActionListener() rn rn public void actionPerformed(final ActionEvent evt) rn rn text_pwdActionPerformed(evt); // 调用方法响应事件操作rn rn );rn text_pwd.setName("text_pwd");rn text_pwd.setBounds(74, 50, 150, 25);rn rn return text_pwd;rn rn /**rn * @returnrn */rn protected JButton getBtn_enter() rn rn if (btn_login==null) rn btn_login=new JButton();rn btn_login.addActionListener(new ActionListener() rn public void actionPerformed(final ActionEvent evt) rn btn_loginActionPerformed(evt); // 调用方法响应事件操作rn rn );rn btn_login.setName("btn_login");rn btn_login.setText("登 录");rn btn_login.setBounds(44, 110, 70, 23);rn rn return btn_login;rn rn /**rn * @returnrn */rn protected JButton getBtn_exit() rn rn if (btn_exit == null) rn btn_exit = new JButton();rn btn_exit.addActionListener(new ActionListener() rn public void actionPerformed(final ActionEvent arg0) rn System.exit(0); // 退出系统rn rn );rn btn_exit.setName("btn_exit");rn btn_exit.setText("退 出");rn btn_exit.setBounds(134, 110, 70, 23);rn rn return btn_exit;rn rn rnrn /**rn * 系统登录rn * @param evt 动作事件rn */rn private void btn_loginActionPerformed(java.awt.event.ActionEvent evt) rn rn Connection conn = null; rn Statement st = null; // Statement对象rn ResultSet rs = null; // 结果集对象rn try rn conn = DaoConn.getConnection(); // 建立数据连接rn st = conn.createStatement(); // 创建Statement对象rn String user = text_user.getText().trim(); // 用户名rn String pwd = new String(text_pwd.getPassword()).trim(); // 密码rn String sql = "select * from tb_doctor where d_user='" + userrn + "' and d_password='" + pwd + "'"; rn rs = st.executeQuery(sql); // 执行查询语句rn if (rs.next() && rs.getString("d_user") != null)rn // 用户名和密码正确 // 用户权限rn // SaveUserInfo.setUserName(user); // 保存用户名到公共类rn //SaveUserInfo.setPassword(pwd); // 保存密码到公共类rn //SaveUserInfo.setQuanXian(quanxian); // 保存用户权限到公共类rn //MainDaohangFrame.launch(); // 启动主窗体rn hideLoginFrame(); // 隐藏登录窗体rn rn else rn rn JOptionPane.showMessageDialog(this, "用户名或密码不正确!!!");rn text_user.setText(""); // 清空文本框rn text_pwd.setText(""); // 清空密码框rn text_user.requestFocus(); // 使用文本框获得输入焦点rn rn rn catch (Exception ex) rn JOptionPane.showMessageDialog(this, ex.getMessage(), "数据库异常",rn JOptionPane.INFORMATION_MESSAGE); // 显示消息框rn rn finally rn try rn conn.close(); // 关闭数据库连接rn rn catch (Exception e) rn rn rn rn rn rn rn /**rn * 用户名文本框的动作事件rn * @param evt 动作事件rn */rn private void text_userActionPerformed(java.awt.event.ActionEvent evt) rn rn text_pwd.requestFocus(); // 密码框获得输入焦点rn rn rn /**rn * 密码框的动作事件rn * @param evt 动作事件rn */rn private void text_pwdActionPerformed(java.awt.event.ActionEvent evt) rn rn btn_loginActionPerformed(evt); // 调用登录按钮的动作事件rn rnrn rn rn rn[/code]rnrn有点长... 麻烦各位啦...
JFrame 和JPanel 的关系-区别(学习笔记)
在学习图形用户界面的时候,搞清楚 JFrame 和 JPanel是非常关键的下面分别给出两个例子加以区分 JFrame 的使用: java.awt.*javax.swing.*Frame1 {     (String[] args) {         JFrame f = JFrame();         f.setSize();         f.setVisible();        ...
JFrame,JPanel默认哪些布局?
JFrame,JPanel默认哪些布局?
如果在JFrame里调用JPanel里的KeyListener事件.
本人在JPanel里implements了KeyListener事件.rn之后在JFrame里add了JPanel,但却不能调用KeyListener事件.rn请问应该怎样写才行呢?
JPanel修改所在JFrame的title
我有一个JFrame frame, 一个JPanel panel,rnrn在frame中通过this.setContentPane(panel);rn把panel添加到了该frame中rnrn现在,我在panel里面有一个按钮,button, 用来改变frame的标题rn panel里面有一个textfield, windowNameTextField用来读取用户输入的字符,然后把它设置成标题rnrnpublic void actionPerformed(ActionEvent e) rn rn String windowName = windowNameTextField.getText();rn rnrn请问,我在上面这个方法里面还应该怎么写呢?
java中jframe 和jpanel的区别
java中jframe 和jpanel的区别
给一个JPanel添加一个键盘事件!怎么添加(在线等)
给一个JPanel添加一个键盘事件!怎么添加
怎么把两个JPANEL的东西放到一个JPANEL里?
怎么把两个JPANEL的东西放到一个JPANEL里?
java之JFrame、JPanel混合布局
package test; import java.awt.*; import javax.swing.*; public class demo extends JFrame{ private JButton[] jb = new JButton[9]; private JPanel jp1, jp2; public static void main(String[] args)
JFrame、JApplet、Jpanel综合测试类
package WindowsandApplet; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JPanel; public class Consoletest { public static String title(Object o) { String s = o.getClass...
Java中的JFrame与JPanel问题?
public class DicomMain extends JFramernrnrn public DicomMain() rnrnrn setSize(1280, 1024);rn setTitle("Read Test");rn DicomTest panel = new DicomTest();rn setContentPane(panel);rnrnrn rnrnrn public static void main(String[] args) rnrnrnrnrn DicomMain frame = new DicomMain();rn frame.getContentPane().setLayout(null);rnrnrn frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rn frame.setVisible(true);rnrnrn rnrnclass DicomTest extends JPanelrnpublic DicomTest()rnrn //这样运行结果不显示按钮rn JPanel Mainpanel=new JPanel();rnrn JButton ReadButton=new JButton("ReadCT");rn ReadButton.setBounds(1150,20,80,40);rn ReadButton.addActionListener(new ReadButtonAction());rn Mainpanel.add(ReadButton);rn//改成下面所示的,显示。为什么?rn JButton ReadButton=new JButton("ReadCT");rn ReadButton.setBounds(1150,20,80,40);rn ReadButton.addActionListener(new ReadButtonAction());rn add(ReadButton);rnrn
设计一个含有JPanel的自定义JFrame类,要求JPanel的尺寸为JFrame的一半,并且永远居中
设计一个含有JPanel的自定义JFrame类,要求JPanel的尺寸为JFrame的一半,并且永远居中
相关热词 c#检测非法字符 c#双屏截图 c#中怎么关闭线程 c# 显示服务器上的图片 api嵌入窗口 c# c# 控制网页 c# encrypt c#微信网页版登录 c# login 居中 c# 考试软件