当前位置: 代码迷 >> Java相关 >> [em13]怎样在JFrame中增加按钮?(我是用JDK写的)
  详细解决方案

[em13]怎样在JFrame中增加按钮?(我是用JDK写的)

热度:290   发布时间:2006-01-06 15:21:00.0
[em13]怎样在JFrame中增加按钮?(我是用JDK写的)

我安装了JDK,在文本编辑器中定的代码
在JFrame中加入了三个按钮,进行窗口,是空白的
在窗口上要按下鼠标才能显示按钮,
并且只显示最后的增加的铵钮,请问这是为什么
要如何时写代码?
import javax.swing.*;
public class c extender JFrame{
JButton jb1 = new JButton("abc1");
JButton jb2 = new JButton("abc2");
JButton jb3 = new JButton("abc3");
public c (){

super("titel")
add(jb1);
add(jb2);
add(jb3);

}
public static void main(String arg[]){
c cc = new c();
cc.show();
}
}

搜索更多相关的解决方案: JDK  JFrame  按钮  

----------------解决方案--------------------------------------------------------
直接在JFrame中加按钮不大好吧???

----------------解决方案--------------------------------------------------------
不在JFrame中加,那要如何时加啊?
----------------解决方案--------------------------------------------------------

import javax.swing.*; //插入javax.swing包
import java.awt.*; //插入java.awt包

class FrameTest extends JFrame
{
public FrameTest() //创建构造符函数
{
super("窗体标题"); //创建JFrame类对象
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); //设置窗体可见
setSize(240,90); //设置窗体大小
}
}
class ButtonTest extends FrameTest
{
JButton buttonObj1;
JButton buttonObj2;
public ButtonTest()
{
buttonObj1=new JButton("按钮1");
buttonObj2=new JButton("按钮2");
getContentPane().add("West",buttonObj1);
getContentPane().add("East",buttonObj2);
}
}
public class J03_Button extends ButtonTest
{
public static void main(String[] args)
{
new J03_Button();
}
}


----------------解决方案--------------------------------------------------------
楼上的不用写这么多类...看着烦

程序代码:

package test;
import java.awt.*;
import javax.swing.*;
/**
* @author hyvin
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class JButtonDemo extends JFrame {
JButton button1;
JButton button2;
JPanel panel1;
JPanel panel2;
JPanel contentPane;
GridLayout gridLayout;
public JButtonDemo(){
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jinit();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
private void jinit(){
contentPane = (JPanel) getContentPane();
GridLayout gridLayout = new GridLayout();
contentPane.setLayout(gridLayout);
button1 = new JButton(\"button1\");
button2 = new JButton(\"button2\");
panel1 = new JPanel();
panel2 = new JPanel();
panel1.add(button1);
panel2.add(button2);
contentPane.add(panel1);
contentPane.add(panel2);
}
public static void main(String[] args) {
JButtonDemo jbd = new JButtonDemo();
jbd.setTitle(\"Demo\");
jbd.pack();
jbd.setVisible(true);
}
}

----------------解决方案--------------------------------------------------------

因为你没写布局,所以只显示最后一个


----------------解决方案--------------------------------------------------------
居然能直接加在jframe上,呵呵 没试过。
建议楼主用可视化编程工具,这样会简单好多。呵呵
----------------解决方案--------------------------------------------------------