你好,在構造方法FrameOut()中調(diào)用setLayout()方法加入一種控件布局形式,例如加入setLayout(new FlowLayout());即可以流布局的形式顯示控件。
創(chuàng)新互聯(lián)建站主要為客戶提供服務項目涵蓋了網(wǎng)頁視覺設計、VI標志設計、成都全網(wǎng)營銷、網(wǎng)站程序開發(fā)、HTML5響應式網(wǎng)站建設、手機網(wǎng)站制作、微商城、網(wǎng)站托管及成都網(wǎng)站維護公司、WEB系統(tǒng)開發(fā)、域名注冊、國內(nèi)外服務器租用、視頻、平面設計、SEO優(yōu)化排名。設計、前端、后端三個建站步驟的完善服務體系。一人跟蹤測試的建站服務標準。已經(jīng)為成都辦公窗簾行業(yè)客戶提供了網(wǎng)站設計服務。
完整代碼如下:
import java.awt.*;
import java.awt.event.*;
public class ApplicationInOut{
public static void main(String args[ ]){
new FrameInOut();
}
}
class FrameInOut extends Frame implements ActionListener{
Label prompt;
TextField input,output;
FrameInOut( ){
super("圖形界面的Java Application程序");
prompt=new Label("Java 是面向?qū)ο蟮恼Z言嗎?");
input=new TextField(6);
output=new TextField(20);
add(prompt);
add(input);
add(output);
input.addActionListener(this);
setLayout(new FlowLayout()); //此處即為添加布局形式
setSize(800,600);
setVisible(true); //show( );
}
public void actionPerformed(ActionEvent e){
output.setText(input.getText()+"OK!");
}
}
試一下下面的代碼
(如果點擊按鈕后沒有任何變化,將窗口最小化一下就有了)
沒有出現(xiàn)這個問題的話,也請告訴我一下~
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class painting extends JFrame implements ActionListener{
private JButton round,rectangle,ellipse,beeline;
private JLabel xaxis,yaxis,remain,information;
private JTextField xTF,yTF;
private BorderLayout layout;
private Container cp;
private JPanel pCenter;
VectorObject v=new VectorObject(); //定義一個集合類用于存儲按鈕對象
public painting(){ //構造方法 ------------------框架初始化-------------------
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("painting");
setSize(400,500);
layout = new BorderLayout();
cp = getContentPane();
cp.setLayout(layout);
round= new JButton("畫圓");
rectangle= new JButton("畫矩形");
ellipse= new JButton("畫橢圓");
beeline= new JButton("畫直線");
xaxis=new JLabel("x坐標");
yaxis=new JLabel("y坐標");
remain=new JLabel("右下角坐標(400,500) ");
xTF=new JTextField("0",5);
yTF=new JTextField("0",5);
JPanel pUp= new JPanel();//第一個面板 在上部
pUp.add(remain);
pUp.add(xaxis);//置兩個文本框
pUp.add(xTF);
pUp.add(yaxis);
pUp.add(yTF);
cp.add(pUp, "North");
//pCenter=new JPanel();//第二個面板 在中部
//pCenter.add(information);//置顯示說明與畫圖區(qū)
//cp.add(pCenter,"Center");
JPanel pDown= new JPanel();//第三個面板 在下部
pDown.add(round);// 置四個按鈕
pDown.add(rectangle);
pDown.add(ellipse);
pDown.add(beeline);
cp.add(pDown, "South");
round.addActionListener(this); //置按鈕監(jiān)聽--------------按鈕行為監(jiān)聽與響應-------------
rectangle.addActionListener(this);
ellipse.addActionListener(this);
beeline.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {//監(jiān)聽響應
v.add(e.getSource());//將按鈕情況存入v中
}
public void paint(Graphics g) { //--------------繪圖響應-------------
super.paint(g);
int xx=Integer.parseInt(xTF.getText());//獲取位置值
int yy=Integer.parseInt(yTF.getText());
int size=0;
Object o;
//while(v.size()!=size){//當用戶點擊按鈕選擇某一種圖形時,v的大小就會比size值大1,當繪圖完成后,v.size又等于size;效果就是:出現(xiàn)點擊 即刻處理
o=v.lastElement();
if(o == round) {g.drawOval(xx,yy,50,50);}
else if (o == rectangle){g.drawRect(xx,yy,100,50);}
else if (o == ellipse) {g.drawOval(xx,yy,100,50);}
else if(o == beeline) {g.drawLine(xx,yy,xx+100,yy);}
size++;
}
}
public static void main(String[] args){ // ------------程序入口-------------
JFrame frame = new painting();
frame.setVisible(true);
}
}
把frame=new subJFrame("DrawShapes");改成frame=new JFrame("DrawShapes");
程序基本沒問題,在public void paint(Graphics g)中加上如下程序就可以了。
public void paint(Graphics g){
switch(i){
case 1: g.drawOval(20,20,40,40);break;
case 2: g.drawRect(20,20,40,40);break;
case 3: g.drawOval(20,30,40,50);break;
case 4: g.drawLine(20,20,40,40);break;
}
}
import?java.awt.Container;
import?java.awt.FlowLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.SwingUtilities;
class?aa?implements?ActionListener
{
JButton?a,?b;
public?aa(JButton?c,?JButton?d)
{
a?=?c;
b?=?d;
}
public?void?actionPerformed(ActionEvent?e)
{
a.setVisible(false);
b.setVisible(false);
}
}
public?class?J1?extends?JFrame
{
private?static?final?long?serialVersionUID?=?1L;
JButton?j1?=?new?JButton("Game1");
JButton?j2?=?new?JButton("Game2");
JButton?j3?=?new?JButton("點擊搖骰子");
J1()
{
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,?400);
setVisible(true);
final?Container?c?=?getContentPane();
c.setLayout(new?FlowLayout());
c.add(j1);
c.add(j2);
j1.addActionListener(new?ActionListener()
{
public?void?actionPerformed(ActionEvent?arg0)
{
c.add(new?JLabel("Game1"));
j3.setVisible(true);
c.add(j3);
}
});
j1.addActionListener(new?aa(j1,?j2));
j2.addActionListener(new?aa(j1,?j2));
j3.addActionListener(new?ActionListener()
{
public?void?actionPerformed(ActionEvent?e)
{
int?a?=?(int)?(Math.random()?*?6);
switch(a)
{
case?0:
c.add(new?JLabel("1"));
break;
case?1:
c.add(new?JLabel("2"));
break;
case?2:
c.add(new?JLabel("3"));
break;
case?3:
c.add(new?JLabel("4"));
break;
case?4:
c.add(new?JLabel("5"));
break;
case?5:
c.add(new?JLabel("6"));
break;
}
SwingUtilities.updateComponentTreeUI(c);
}
});
}
public?static?void?main(String[]?args)
{
new?J1();
}
}
dty@ubuntu:~$ cat CC.java
import?java.awt.BorderLayout;
import?java.awt.Color;
import?java.awt.Container;
import?java.awt.FlowLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
public?class?CC?extends?JFrame?implements?ActionListener?{
private?JPanel?jp?=?null;
private?JButton?b1?=?null,?b2?=?null;
public?CC()?{
Container?c?=?this.getContentPane();
//創(chuàng)建panel
jp?=?new?JPanel();
//為panel設置底色
jp.setBackground(Color.red);
//jp用流水布局
jp.setLayout(new?FlowLayout());
//創(chuàng)建按鈕
b1?=?new?JButton("紅色");
b2?=?new?JButton("藍色");
/**
*?添加按鈕事件
*/
b1.addActionListener(this);
b2.addActionListener(this);
/**
*?將按鈕添加相應panel上
*/
jp.add(b1);
jp.add(new?JLabel());
jp.add(b2);
/**
*?將panel添加到容器
*/
c.add(jp,?BorderLayout.CENTER);
this.setSize(500,?500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}????
public?static?void?main(String[]?args)?{
new?CC();????
}
@Override
public?void?actionPerformed(ActionEvent?e)?{
//?TODO?Auto-generated?method?stub
if?(e.getSource()?==?b1)?{
jp.setBackground(Color.red);
}?else?if?(e.getSource()?==?b2)?{
jp.setBackground(Color.blue);
}
}
}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
//暫時不用考慮連加問題
//點第一個運算符 點運算符 點第二個運算符 點=出結果
public class 計算器 implements ActionListener {
JTextField jtf = new JTextField(10);
private boolean append = false;
private String op1 = "0";
private String operator = "+";
@Override
public void actionPerformed(ActionEvent e) {
String comn = e.getActionCommand();
// 處理數(shù)字
if ("0123456789".indexOf(comn) != -1) {
if (append) {// 追加
String temp = jtf.getText();
jtf.setText(temp + comn);
} else {// 替換
jtf.setText(comn);
append = true;
}
}
// 處理運算符
else if ("+-*/".indexOf(comn) != -1) {
op1 = jtf.getText();
operator = comn;
append = false;
} else if ("=".indexOf(comn) != -1) {
String op2 = jtf.getText();
double d1 = Double.parseDouble(op1);
double d2 = Double.parseDouble(op2);
if ("+".equals(operator)) {
d1 = d1 + d2;
} else if ("-".equals(operator)) {
d1 = d1 - d2;
} else if ("*".equals(operator)) {
d1 = d1 * d2;
} else if ("/".equals(operator)) {
d1 = d1 / d2;
}
jtf.setText(d1 + "");
append = false;
} else if (".".equals(comn)) {
String temp = jtf.getText();
if (temp.indexOf(".") == -1) {
jtf.setText(temp + ".");
append = true;
}
} else if ("+/-".equals(comn)) {
String temp = jtf.getText();
if (temp.startsWith("-1")) {
jtf.setText(temp.substring(1));
} else {
jtf.setText("-" + temp);
}
} else if ("Backspace".equals(comn)) {
String temp = jtf.getText();
if (temp.length() 0) {
jtf.setText(temp.substring(0, temp.length() - 1));
}
} else if ("CE".equals(comn) || "C".equals(comn)) {
jtf.setText("0");
append = false;
}
}
public 計算器() {
JFrame jf = new JFrame("計算器");
jf.add(jtf, BorderLayout.NORTH);
String[] s1 = { "Backspace", "CE", "C", "+", "7", "8", "9", "/", "4",
"5", "6", "*", "1", "2", "3", "-", "0", "+/-", ".", "=" };
JPanel jp = new JPanel();
jf.add(jp, BorderLayout.CENTER);
GridLayout gl = new GridLayout(5, 4);
jp.setLayout(gl);
JButton[] jb = new JButton[s1.length];
for (int i = 0; i s1.length; i++) {
jb[i] = new JButton(s1[i]);
jp.add(jb[i]);
jb[i].addActionListener(this);
}
jf.add(jp);
jtf.setEditable(false);
jf.setLocation(400, 300);
jf.pack();
jf.setResizable(false);// 設置窗口的大小不可變
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
new 計算器();
}
}
這個功能比較簡單 不知道能不能滿足要求