「javaswing试题」javaswing菜鸟教程
本篇文章给大家谈谈javaswing试题,以及javaswing菜鸟教程对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
一道简单的Java Swing问题
if(e.getSource==b1)
{
System.exit(0);
}
这样就可以了。
或是在子窗口中设置setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
这样当通过主窗口打开子窗口时,再关闭子窗口就不会影响主窗口了。
java试题:给你一个1――100之间的整数,请猜测这个数。求代码
import javax.swing.JOptionPane;
public class GuessNumber
{
public static void main (String args[ ])
{
guess();
}
static void guess(){
System.out.println("给你一个1至100之间的整数,请猜测这个数");
int realNumber=(int)(Math.random()*100)+1;
int yourGuess=0;
String str=JOptionPane.showInputDialog("输入您的猜测:");
yourGuess=Integer.parseInt(str);
while(yourGuess != realNumber) //循环条件
{
if(yourGuess realNumber) //条件代码
{
str=JOptionPane.showInputDialog("猜大了,再输入你的猜测:");
yourGuess=Integer.parseInt(str);
}
else if(yourGuess realNumber) //条件代码
{
str=JOptionPane.showInputDialog("猜小了,再输入你的猜测:");
yourGuess=Integer.parseInt(str);
}
}
System.out.println("猜对了!");
}
}
急!!求一套Java试题答案
单选:
1.D 2.B 3.B 4.B 5.D 6.B 7.D 8.A
多选:
1.BC 2.D(B肯定不对) 3.ABCD 4.BCDE;
判断对错:
1.对 2.对 3.对 4.对 5.对 6.错 7.错 8.错 9.错
JAVA 有关Swing绘图的一道习题。
敲代码满了,来晚了一步==||,俺初学,这程度了……
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class TrafficLightPanel extends JPanel {
private int sec = 0;// 计时的秒数,用于判断循环到哪个灯
private String countdownStr = "00";
private int countdown = 0;// 倒计时
private static final int GREEN_LIGHT = 0;// 绿灯
private static final int YELLOW_LIGHT = 1;// 黄灯
private static final int RED_LIGHT = 2;// 红灯
private int light = 0;
private Timer timer;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new TrafficLightPanel());
frame.setBounds(0, 0, 100, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(3);
}
public TrafficLightPanel() {
timer = new Timer(1000, listener);
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(new Font("宋体", Font.BOLD, 12));// 设置字体
g
.fillRect(this.getWidth() / 2 - 15, this.getHeight() / 2 - 50,
30, 100);// 画矩形
if (light == GREEN_LIGHT) {// 如果是绿灯则画绿色的圆,下同
g.setColor(Color.green);// 设置绿色
g.fillOval(this.getWidth() / 2 - 10, this.getHeight() / 2 - 45, 20,
20);// 话圆
g.setColor(Color.white);// 设置白色
g.drawString(String.valueOf(countdownStr), this.getWidth() / 2 - 5,
this.getHeight() / 2 - 28);// 画字
} else {
g.setColor(Color.gray);
g.fillOval(this.getWidth() / 2 - 10, this.getHeight() / 2 - 45, 20,
20);
}
if (light == YELLOW_LIGHT) {
g.setColor(Color.yellow);
g.fillOval(this.getWidth() / 2 - 10, this.getHeight() / 2 - 15, 20,
20);
g.setColor(Color.white);
g.drawString(String.valueOf(countdownStr), this.getWidth() / 2 - 5,
this.getHeight() / 2 + 2);
} else {
g.setColor(Color.gray);
g.fillOval(this.getWidth() / 2 - 10, this.getHeight() / 2 - 15, 20,
20);
}
if (light == RED_LIGHT) {
g.setColor(Color.red);
g.fillOval(this.getWidth() / 2 - 10, this.getHeight() / 2 + 15, 20,
20);
g.setColor(Color.white);
g.drawString(String.valueOf(countdownStr), this.getWidth() / 2 - 5,
this.getHeight() / 2 + 30);
} else {
g.setColor(Color.gray);
g.fillOval(this.getWidth() / 2 - 10, this.getHeight() / 2 + 15, 20,
20);
}
}
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
sec++;
if (sec == 1) {// 1-10秒为绿灯,下同
light = GREEN_LIGHT;
countdown = 10;// 10秒倒计时
} else if (sec == 11) {
light = YELLOW_LIGHT;
countdown = 5;
} else if (sec == 16) {
light = RED_LIGHT;
countdown = 15;
} else if (sec == 31) {
light = YELLOW_LIGHT;
countdown = 5;
} else if (sec == 35) {
sec = 0;
}
countdown--;
countdownStr = countdown 10 ? "0" + countdown : "" + countdown;
update(getGraphics());// 重新绘制画面
}
};
}
关于javaswing试题和javaswing菜鸟教程的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-11,除非注明,否则均为
原创文章,转载请注明出处。