我目前正在为一个程序编写代码,当它运行时,会打开一个外部窗口,上面有一个建筑物和一个滚动横幅。如果您在建筑物上方的文本框中输入短语,该短语将滚动横幅。
DisplayWindow
import javax.swing.*;
import java.awt.*;
public class DisplayWindow extends JFrame {
private Container c;
public DisplayWindow() {
super("Display");
c = this.getContentPane();
}
public void addPanel(JPanel p) {
c.add(p);
}
public void showFrame() {
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
MovingSignPanel
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MovingSignPanel extends JPanel implements ActionListener{
JMenuBar b;
JButton start = new JButton("Start");
JButton stop = new JButton("Stop");
JButton quit = new JButton("Quit");
JTextField phrase = new JTextField(20);
private int lVar = 200;
private int rVar = 600;
private int hVar = 200;
private int ground = 400;
private Timer scroll = new Timer(40,this);
private int xVel = 2;
private int xVal = lVar;
private int yVal = 150;
private String input = " ";
private int inputWidth = 0;
private Boolean scrolling = false;
public MovingSignPanel(JMenuBar b){
setPreferredSize(new Dimension(1000, 1000));
setBackground(Color.white);
this.add(phrase);
phrase.addActionListener(this);
this.add(start);
start.addActionListener(this);
this.add(stop);
stop.addActionListener(this);
this.add(quit);
quit.addActionListener(this);
}
public void drawBanner(Graphics g){
clearBanner(g);
drawBuilding(g);
int position = xVal;
while(position < rVar){
g.drawString(input,position,yVal);
position += inputWidth;
}
position = xVal - inputWidth;
while(position > lVar - inputWidth){
g.drawString(input,position,yVal);
position -= inputWidth;
}
if(xVal > rVar)
xVal -= inputWidth;
xVal += xVel;
drawBuilding(g);
}
public void drawBuilding(Graphics g){
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0,0,1000,1000);
g.setColor(Color.gray);
g.fillRect(lVar,200,rVar-lVar,hVar);
g.fillRect(lVar,100,rVar-lVar,hVar-800);
g.setColor(Color.lightGray);
g.fillRect(0, ground, 700, 400 - ground);
g.setColor(Color.blue);
for(int n = lVar + 20; n < rVar - 10; n += 40){
for(int m = 60; m < 150; m += 30){
g.fillRect(n,m,20,20);
}
}
for(int n = lVar + 20; n < rVar - 10; n += 40){
for(int m = 210; m < 350; m += 30){
g.fillRect(n,m,20,20);
}
}
g.setColor(Color.darkGray);
g.fillRect(0,0,lVar,ground);
g.fillRect(rVar,0,lVar,ground);
}
public void inputMsg(){
input = phrase.getText();
inputWidth = phrase.getText().length();
}
public void resetTimer(){
scroll.stop();
scrolling = false;
}
public void startMsg(){
inputMsg();
if(!scrolling){
scroll.start();
scrolling = true;
}
}
public void clearBanner(Graphics g){
g.clearRect(0,0,1000,1000);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == quit)
System.exit(0);
if (e.getSource() == start)
repaint();
startMsg();
if (e.getSource() == stop)
resetTimer();
}
}
SignDriver
import javax.swing.*;
public class SignDriver {
public static void main(String[] args) {
DisplayWindow d = new DisplayWindow();
JMenuBar menuBar = new JMenuBar();
d.setJMenuBar(menuBar);
MovingSignPanel p = new MovingSignPanel(menuBar);
d.addPanel(p);
d.showFrame();
}
}
如果需要,我可以更好地组织这个。我知道按钮还没有工作,但是现在我更关心为什么在程序运行时没有任何东西被绘制出来。每当我尝试运行程序时都会出现错误,如下所示:
java.lang.NoSuchMethodError: MovingSignPanel.<init>(Ljavax/swing/JMenuBar;)V
at SignDriver.main(SignDriver.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
答案 0 :(得分:2)
似乎你的SignDriver
类在运行时有一个旧版本的类MovingSignPanel
,这个版本没有这个构造函数MovingSignPanel(javax.swing.JMenuBar)
。只是尝试清理和重建,这个错误应该消失。