导航:首页 > 编程语言 > javaswing画座位

javaswing画座位

发布时间:2023-03-15 16:29:32

1. java中swing绘制图形问题

//完整程序如下

import java.awt.*;
import javax.swing.*;

public class DrawPolygon extends JFrame {
boolean closed;
boolean filled;
int[] x,y;
int count;
boolean canDraw=false;

public DrawPolygon() {
super("画多边形");
setBounds(100,100,400,300);
count=0;
setVisible(true);
while (count==0) {
try {
count=Integer.parseInt(JOptionPane.showInputDialog(this,"输入多边形点个数","顶点",JOptionPane.INFORMATION_MESSAGE));
if (count<3 || count>10) {
JOptionPane.showMessageDialog(this,"输入的顶点数不在3-10范围之内","顶点",JOptionPane.ERROR_MESSAGE);
count=0;
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(this,"输入的不是合法整数","顶点",JOptionPane.ERROR_MESSAGE);
}
}
x=new int[count];
y=new int[count];
for (int i=0; i<count; i++) {
y[i]=0;
while (y[i]==0) {
try {
String s=JOptionPane.showInputDialog(this,"输入第"+String.valueOf(i+1)+"个点的坐标:\n例如:80,120","顶点",JOptionPane.INFORMATION_MESSAGE);
if (s==null) {
return;
}
x[i]=Integer.parseInt(s.substring(0,s.indexOf(',')));
y[i]=Integer.parseInt(s.substring(s.indexOf(',')+1));
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(this,"输入的点坐标不合法","顶点",JOptionPane.ERROR_MESSAGE);
}
}
}
closed=false;
filled=false;
if (JOptionPane.showConfirmDialog(this,"是否封闭图形?","封闭",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION) {
closed=true;
}
if (JOptionPane.showConfirmDialog(this,"是否填充图形?","填充",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION) {
filled=true;
}
canDraw=true;
repaint();
}
public void paint(Graphics g) {
g.clearRect(0,0,getWidth(),getHeight());
if (canDraw && count>=3 && count<=10) {
if (filled) {
g.fillPolygon(x,y,count);
}
if (closed) {
g.drawPolygon(x,y,count);
} else {
g.drawPolyline(x,y,count);
}
}
}

public static void main(String[] args) {
new DrawPolygon();
}
}

2. java swing窗口 绘图

每个Swing组件都用一个方法:
protected void paintComponent(Graphics g)

可以通过重写这个方法还绘图。
Graphics对象是这个方法的入口参数,不用获得,在重写的方法内直接使用就可以。

给你个例子。

http://student.csdn.net/space.php?uid=209919&do=blog&id=36792

3. 用SWING包编写一段代码重复地在屏幕上的随机位置用随机颜色画随机尺寸的圆形和矩形(形状也是随机出现),

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.*;
import java.util.Map.Entry;
import java.util.Random;
import javax.swing.*;

public class RandomShapeDemo extends JFrame {

public RandomShapeDemo() {
super("Random Shape Demo");
shapes = Collections.synchronizedMap(new HashMap<Shape, Color>());

this.setContentPane(new JPanel() {

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//画出当前所源哪有生销裂咐成好的图形
try {
for (Entry<Shape, Color> entry : shapes.entrySet()) {
g2d.setColor(entry.getValue());
g2d.fill(entry.getKey());
}
} catch (Exception e) {
}
}
});

this.setSize(800, 600);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setVisible(true);
}

public static void main(String[] args) {
RandomShapeDemo demo = new RandomShapeDemo();
demo.setVisible(true);
new RandomFillShapeThread(demo).start();

}

/**
* 随机生成新的图形
*/
public void fillShape() {
Random random = new Random();
int x = random.nextInt(800);
int y = random.nextInt(600);
int radius = random.nextInt(100);
//随机颜亏纯色
Color c = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
//随机图形
Shape circle1 = new Ellipse2D.Double(x, y, radius, radius);
Shape circle2 = new Rectangle2D.Double(x, y, radius, radius);
//添加入表中
if((random.nextInt(256))%2==0)
{
shapes.put(circle1, c);
}else{
shapes.put(circle2, c);
}
//刷新面板
repaint();
}

private static class RandomFillShapeThread extends Thread {

private RandomFillShapeThread(RandomShapeDemo demo) {
this.demo = demo;
}

@Override
public void run() {
//当demo面板还在显示的时候
while (demo.isVisible()) {
//向demo添加新的随机图形
demo.fillShape();
//休眠一段时间
try {
sleep(SLEEP_TIME);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
try {
//释放demo
demo.dispose();
} catch (Exception e) {
}
}
private static final long SLEEP_TIME = 1000L;
private RandomShapeDemo demo;
}

private Map<Shape, Color> shapes;
}

网上找了一部分 而后又改的一部分 正好是我的机试题 希望可以帮到你

4. java swing 组件的绘制问题

paintComponet vs paintBorder vs paintChildren:
如果你看JComponent.paint(), 你会发现上面三个method是按顺序调用的(顺序不能错)。
paintComponent用来画一个容器(Container)的背景,paintBorder用来画边缘,
paintChildren用来画容器(Container)里包含的组件。
所以该用什么在于你的程序了。

5. java swing里怎么布局....求各位

最简单的:将源迟贺JPanel的布局设旦培为null,设定按钮的bounds就可以了.

JPanel p=new JPanel();
p.setLayout(null);
JButton b1=new JButton("确定雹派");
b1.setBounds(10,10,30,20);
JButton b2=new JButton("取消");
b2.setBounds(50,10,30,20);
p.add(b1);
p.add(b2);

6. java swing布局

布局设成null,其中的组件都设定位置

import javax.swing.*;

public class LoadDialog extends javax.swing.JDialog {
private JPanel outer;
private JLabel one;
private JLabel three;
private JPasswordField pw3;
private JPasswordField pw2;
private JPasswordField pw1;
private JLabel two;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
LoadDialog inst = new LoadDialog(null);
inst.setResizable(false);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

public LoadDialog(JFrame frame) {
super(frame);
initGUI();
}

private void initGUI() {
try {
setTitle("修改密码:");
getContentPane().setLayout(null);
{
outer = new JPanel();
getContentPane().add(outer);
outer.setBounds(41, 34, 313, 194);
outer.setBorder(BorderFactory.createTitledBorder("修改信息:"));
outer.setLayout(null);
{
one = new JLabel();
outer.add(one);
one.setText("输入原密码:");
one.setBounds(44, 56, 69, 15);
}
{
two = new JLabel();
outer.add(two);
two.setText("输入新密码:");
two.setBounds(44, 90, 69, 15);
}
{
three = new JLabel();
outer.add(three);
three.setText("重复原密码:");
three.setBounds(44, 126, 69, 15);
}
{
pw1 = new JPasswordField();
outer.add(pw1);
pw1.setText("");
pw1.setBounds(119, 52, 133, 22);
}
{
pw2 = new JPasswordField();
outer.add(pw2);
pw2.setText("");
pw2.setBounds(119, 86, 133, 22);
}
{
pw3 = new JPasswordField();
outer.add(pw3);
pw3.setText("");
pw3.setBounds(119, 122, 133, 22);
}
}
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}

}

7. 求助java的swing窗体怎么做如图布局效果

运行效果


importjava.awt.Container;
importjava.awt.Font;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.Box;
importjavax.swing.BoxLayout;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.SwingUtilities;
{
privateJLabelnumLabel=newJLabel("15");
privateJButtonstartBtn=newJButton("Start");
privateJButtonstopBtn=newJButton("Stop");
privateJButtonspeedUpBtn=newJButton("SpeedUp");
privateJButtonslowDownBtn=newJButton("SlowDown");
{
startBtn.addActionListener(this);
stopBtn.addActionListener(this);
speedUpBtn.addActionListener(this);
slowDownBtn.addActionListener(this);
Containercon=getContentPane();
con.setLayout(newBoxLayout(con,BoxLayout.Y_AXIS));
numLabel.setFont(newFont("",Font.BOLD,60));
BoxnumBox=Box.createHorizontalBox();
numBox.add(numLabel);
add(numBox);
BoxbtnBox=Box.createHorizontalBox();
btnBox.add(startBtn);
btnBox.add(stopBtn);
btnBox.add(speedUpBtn);
btnBox.add(slowDownBtn);
add(btnBox);

setTitle("ThreadTest");
setSize(400,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEvente){

}
publicstaticvoidmain(String[]args){
SwingUtilities.invokeLater(()->newTestWin());
}
}

8. java swing 组件的坐标问题

首先把面板的Layout设为null
例jPanel.setLayout(null);
然后对于单个组件的bounds设为你想要的坐标
例jLabel.setBounds(new Rectangle(100, 100, 100, 40));
其中4个参数分别为组件的左上角横坐标,纵坐标,横长和纵宽
jList.setSize(100,50)

9. JAVA swing 布局,求高手、、、

下面应该是textfield吧 设置成为全局变量 private TextField tt = new TextField(), 然后对应anctionForm里面写tt.text("dd")等等。

10. JAVA程序Swing绘图问题

public class fs extends JFrame {
public static void main(String args[]) {
Demo myDemo=new Demo();
}
}
fs类盯困轿为啥也继承JFrame类, Demo类尺销继承JFrame类了,Demo就是一个JFrame了凯肆,赶脚怪怪的。

阅读全文

与javaswing画座位相关的资料

热点内容
铁路12306密码找不回 浏览:352
默认网络覆盖的脑区 浏览:319
itunes恢复iphone教程 浏览:292
炉石现在是什么版本 浏览:825
word兼容包安装报错 浏览:528
iphone5s包装4g没有气孔 浏览:814
html包含文件代码吗 浏览:50
苹果appstore日本账号 浏览:532
解密dg加密的文件 浏览:206
gsh6什么格式文件 浏览:507
dnf85版本觉醒任务 浏览:998
范冰冰苹果百度云盘资源链接 浏览:507
数据库主机是什么系统 浏览:812
pdf表单教程 浏览:715
百度浏览器去更新安卓破解版 浏览:855
光盘内部应用程序错误 浏览:83
iphone6升级ios9步骤 浏览:873
魔力代码 浏览:497
win10打开局域网文件夹很卡 浏览:986
app收益怎么分 浏览:812

友情链接