❶ java怎樣放兩個按鈕在窗體的正中間
JPanel 放入到BorderLayout.CENTER , 那麼會自動填充滿整個contentPane的中間, 而JPanel內部還是流式布局, 一行排滿 自動換到下一行,從上到下. 所以按鈕還是在最上面.
(把JPanel的背景色改成藍色,就可以清晰的看到JPanel填滿了窗口)
importjavax.swing.*;
{
publicJFDemo2(){
JPanelpane=newJPanel();
BoxLayoutlayout=newBoxLayout(pane,BoxLayout.X_AXIS);//水平的盒布局
pane.setLayout(layout);
JButtonmessageButton=newJButton("OK");
JButtoncloseButton=newJButton("Cancel");
pane.add(Box.createGlue());//擠佔ok按鈕和窗口左側空間
pane.add(messageButton);
pane.add(Box.createHorizontalStrut(20));//按鈕之間的水平距離
pane.add(closeButton);
pane.add(Box.createGlue());//擠佔cancel按鈕和窗口右側空間
add(pane);
setTitle("Demo");//標題
setSize(320,230);//窗口大小
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//窗口點擊關閉時,退出程序
}
publicstaticvoidmain(String[]args){
newJFDemo2().setVisible(true);
}
}
總結: 推薦使用方法二,使用盒布局來實現.
一般不推薦使用絕對布局/空布局 來布局窗口, 因為不同的操作系統下顯示的效果不完全一致.
並且還需要寫大量的代碼來計算組件的大小和位置, 當窗口放大和縮小時 還需要重新計算位置
❷ Java編程。比如在一個面板上有兩個按鈕,怎麼實現分別點擊這兩個按鈕然後彈出不同的窗口我的代碼不
importjava.awt.Color;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassTestextendsJFrame{
publicTest(){
this.setSize(400,300);
this.setLocationRelativeTo(null);//設置居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPaneljp=newJPanel();
finalJButtonjb1=newJButton("彈出藍色");
JButtonjb2=newJButton("彈出青色");
jp.add(jb1);
jp.add(jb2);
//給第一個按鈕添加監聽
jb1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
JFramejf=newJFrame();
JPaneljp=newJPanel();
jf.setSize(200,200);
jf.add(jp);
jp.setBackground(Color.blue);
jf.setLocationRelativeTo(jb1);
jf.setVisible(true);
}
});
//給第二個按鈕添加監聽
jb2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
JFramejf=newJFrame();
JPaneljp=newJPanel();
jf.setSize(200,200);
jf.add(jp);
jp.setBackground(Color.cyan);
jf.setLocationRelativeTo(jb1);
jf.setVisible(true);
}
});
this.add(jp);
}
publicstaticvoidmain(Stringarg[]){
newTest().setVisible(true);
}
}
❸ 急!!!高分!java設計兩個按鈕,按下以後顯示不同圖形,幫我看下已經寫了一半的程序
你的程序我幫你改完了,你看看吧.(運行後能達到你的要求)
importjava.awt.*;
importjava.applet.Applet;
importjavax.swing.*;
importjava.awt.event.*;
{
JButtonjr1;
JButtonjr2;
Stringresult="";
intn;
JPaneljp1=newJPanel();
MyPaneljp2=newMyPanel();
publicvoidinit(){
setSize(300,300);
setLayout(newBorderLayout());
jr1=newJButton("circle");
jr2=newJButton("square");
jp1.add(jr1);
jp1.add(jr2);
jp1.setOpaque(false);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
jr1.addActionListener(this);
jr2.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==jr1){
n=1;
repaint();
}
if(e.getSource()==jr2){
n=2;
repaint();
}
}
classMyPanelextendsJPanel{
publicvoidpaint(Graphicsg){
showGraphics(g);
}
publicvoidshowGraphics(Graphicsg){
switch(n){
case1:
g.setColor(Color.blue);
g.drawOval(30,30,160,160);
break;
case2:
g.setColor(Color.orange);
g.fillRoundRect(30,30,160,160,20,120);
break;
}
}
}
}
❹ java作業:一個窗口兩個按鈕,按按鈕分別出來不同的字。。。。。。大神幫忙啊。。。
代碼如下:
importjavax.swing.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
publicclassHelloIFrame{
privatestaticJTextAreaarea;
publicstaticvoidmain(String[]args){
JFramejf=newJFrame();
jf.setTitle("JAVA");
jf.setBounds(500,200,300,300);
JPanelcon=newJPanel(null);
area=newJTextArea();
area.setLineWrap(true);
JScrollPanejp=newJScrollPane(area);
jp.setBounds(10,10,280,200);
con.add(jp);
JButtonhelloButton=newJButton("HELLO!");
JButtonclearButton=newJButton("JAVA");
helloButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
area.setText("");
area.append("Hello!"+" ");
}
});
clearButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
area.setText("");
area.append("JAVA!"+" ");
}
});
helloButton.setBounds(70,220,75,30);
clearButton.setBounds(150,220,75,30);
con.add(helloButton);
con.add(clearButton);
jf.add(con);
jf.setResizable(false);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
如果滿意請採納!
❺ Java 兩個按鈕不同狀態切換
一: 首先對需求進行分析:
兩個不同的按鈕,
兩個事件接收器,
當按鈕被點擊後, 事件接收器接受到點擊信息, 然後進行事件處理邏輯--->修改按鈕的狀態
二: 其次 對需要用的知識進行分析
A. 按鈕狀態的設置
按鈕的狀態----->不可見----->通過設置屬性是否可見實現
JButtonbutton1=newJButton("按鈕一");
button1.setVisible(false);//設置為不可見的狀態
按鈕的狀態----->不可用----->通過設置屬性是否用實現
JButtonbutton2=newJButton("按鈕二");
button2.setEnabled(false);//設置為不可用的狀態
B. 事件的接收器的設置, 已經書寫
一般使用匿名內部類來書寫接收器, 以及處理代碼
button.addActionListener(newActionListener(){//為按鈕添加事件接收器
publicvoidactionPerformed(ActionEvente){
//這里寫事件處理代碼
}
});
三: 效果圖
//導入所需要的包
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
{
//申明需要操作的按鈕控制項
privatefinalJButtonbutton1,button2;
publicFrameDemo(){
setTitle("按鈕測試窗口");//設置窗口標題
setSize(220,80);//設置窗口的大小
setLocationRelativeTo(null);//設置窗口居於屏幕中央
setDefaultCloseOperation(EXIT_ON_CLOSE);//設置點擊關閉時退出虛擬機
setLayout(newFlowLayout());//布局窗口為流式布局
button1=newJButton("按鈕一");
button2=newJButton("按鈕二");
button1.addActionListener(newActionListener(){//為按鈕一設置事件接收器
publicvoidactionPerformed(ActionEvente){
button1.setVisible(false);//當按鈕一被點擊時,該按鈕設置為不可見
}
});
button2.addActionListener(newActionListener(){//為按鈕二設置事件接收器
publicvoidactionPerformed(ActionEvente){
button2.setEnabled(false);//當按鈕二被點擊時,該按鈕設置為不可用
}
});
JPaneljp=newJPanel(newGridLayout(1,2));
jp.add(button1);
jp.add(button2);
add(jp);
}
publicstaticvoidmain(String[]args){
newFrameDemo().setVisible(true);//創建按鈕窗口,並且設置為可見
}
}
五: 拓展思考
java 的源代碼一般都遵循java 的規范,
屬性的設置使用setXXX(...)方法
屬性的獲得使用getXxx(); 方法
使用的時候, 可以很方便的調用
所以, 我們平時開發java的時候也應該遵循java開發的規范. 會給我們的工作帶來很多的便利
❻ 怎麼用java實現在同一界面點擊兩個按鈕進入兩個不同界面
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ButtonJFrame extends JFrame {
ButtonJFrame() {
super("ButtonJFrame");
JButton btnA = new JButton("打開FrameA");
JButton btnB = new JButton("打開FrameB");
this.setLayout(new BorderLayout());
this.getContentPane().add(btnA, BorderLayout.WEST);
this.getContentPane().add(btnB, BorderLayout.EAST);
btnA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FrameA().setVisible(true);
}
});
btnB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FrameB().setVisible(true);
}
});
this.setSize(320, 240);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new ButtonJFrame();
}
class FrameA extends JFrame {
FrameA() {
super("FrameA");
this.setSize(160, 120);
}
}
class FrameB extends JFrame {
FrameB() {
super("FrameB");
this.setSize(160, 120);
}
}
}