单击时滚动条消失,重新调整大小时 - >旧屏幕保留以及新屏幕的一部分出现在斑点中。单击时滚动条消失,当重新调整窗口大小时,中心面板变形(保持旧的过时滚动条并混淆文本)
我正在为我的一个班级创建一个基于旧式学校文本的冒险游戏。我是新手使用摇摆,我的中央框架面板有问题。我遇到问题的代码中唯一的部分是滚动条(请参阅updateScreen()和GraphicView()构造函数)。很抱歉,如果代码难以理解,因为这只是众多课程中的一个,而且我之前尝试过的有相同问题的评论。
如果要运行它,请查看底部的简化代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GraphicView extends View {
public static void main(String[] args)
{
GraphicView view = new GraphicView();
}
GraphicView()
{
frame = new JFrame();
frame.addWindowListener(new GuiWindowListener());
frame.setLocation(100, 200);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Nittany Cubs Gaming GUI");
frame.setLayout(new BorderLayout());
//JScrollPain mainPanel = new Panel();
// mainPanel = new JPanel();
// mainPanel.setLayout(new BorderLayout());
//northButton = new JButton("Go North");
//northButton.addActionListener(new NorthButtonActionListener());
//eastButton = new JButton("Go east");
//eastButton.addActionListener(new EastButtonActionListener());
//southButton = new JButton("Go south");
//southButton.addActionListener(new SouthButtonActionListener());
//westButton = new JButton("Go west");
//westButton.addActionListener(new WestButtonActionListener());
//frame.add(westButton,BorderLayout.WEST);
//frame.add(southButton,BorderLayout.SOUTH);
//frame.add(eastButton,BorderLayout.EAST);
//frame.add(northButton,BorderLayout.NORTH);
// Panel southPanel = new Panel();
// mainPanel.add(southPanel, BorderLayout.SOUTH);
// Panel northPanel = new Panel();
// northPanel.setSize(frame.getWidth(), 50);
// northPanel.setBackground(Color.BLACK);
// add north button to north panel
// Button northButton = new Button("North Button");
// northButton.setSize(northPanel.getWidth()-10, northPanel.getHeight()-10);
//northButton.setSize(northPanel.getSize());
//northButton.setBounds(new Rectangle(northPanel.getBounds()));
//northButton.setMaximumSize(northPanel.getSize());
// northButton.setBackground(Color.PINK);
// northButton.addActionListener(new NorthButtonActionListener());
// northPanel.add(northButton);
// add north panel to main panel
// mainPanel.add(northPanel, BorderLayout.NORTH);
northButton = new JButton("Go North");
northButton.addActionListener(new NorthButtonActionListener());
frame.add(northButton, BorderLayout.NORTH);
eastButton = new JButton("Go east");
eastButton.addActionListener(new EastButtonActionListener());
frame.add(eastButton, BorderLayout.EAST);
southButton = new JButton("Go south");
southButton.addActionListener(new SouthButtonActionListener());
frame.add(southButton, BorderLayout.SOUTH);
westButton = new JButton("Go west");
westButton.addActionListener(new WestButtonActionListener());
frame.add(westButton, BorderLayout.WEST);
text = "";
mainPanel = new JPanel();
textArea = new JTextArea(text);
textArea.setEditable(false);
//mainPanel.add(textArea);
//scroller = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroller = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroller,BorderLayout.CENTER);
updateScreen();
}
private static void updateScreen()
{
// JTextArea ta = new JTextArea(text);
textArea = new JTextArea(text);
//mainPanel.add(textArea);
scroller = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//scroller = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroller,BorderLayout.CENTER);
frame.setVisible(true);
// JScrollPane scroller = new JScrollPane(ta);
// disables editing
// ta.setEditable(false);
// scroller = new JScrollPane(ta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// scroller.setBounds(3, 3, 300, 200);
// label = new JLabel("<html>" + text + "</html>");
// label.setLayout(new BorderLayout());
// label.setVerticalAlignment(SwingConstants.TOP);
// scroller = new JScrollPane(label, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//scroller.addMouseWheelListener(new MyMouseWheelListener());
// scroller.createVerticalScrollBar();
// scroller.createHorizontalScrollBar();
// scroller.setAutoscrolls(true);
// mainPanel.add(scroller, BorderLayout.CENTER);
// frame.getContentPane().add(mainPanel);
// frame.add(scroller,BorderLayout.CENTER);
// frame.setVisible(true);
}
@Override
public void informInvalidGoDirection(String direction) {
text += "\nYou hit your nose on the wall trying to go " + direction + ". ";
updateScreen();
}
@Override
public void informPlayerMoved() {
// TODO Auto-generated method stub
}
@Override
public void look() {
// TODO Auto-generated method stub
Location currentLocation = Application.instance().playerCurrentLocation();
text += currentLocation.description() + " " + getItemsDescription(currentLocation) + " " +
getMobsDescription(currentLocation) + " " +
getCanMoveDirectionsDescription(currentLocation);
updateScreen();
}
private class NorthButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
Location currentLocation = Application.instance().playerCurrentLocation();
boolean isLocationValid = Application.instance().isThereALocationNorthOf(currentLocation);
if(isLocationValid)
{
Application.instance().movePlayerNorth();
Location newLocation = Application.instance().playerCurrentLocation();
text = newLocation.description() +
getItemsDescription(newLocation) +
getMobsDescription(newLocation) +
getCanMoveDirectionsDescription(newLocation);
updateScreen();
}
else
{
informInvalidGoDirection("north");
}
}
}
private class SouthButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
Location currentLocation = Application.instance().playerCurrentLocation();
boolean isLocationValid = Application.instance().isThereALocationSouthOf(currentLocation);
if(isLocationValid)
{
Application.instance().movePlayerSouth();
Location newLocation = Application.instance().playerCurrentLocation();
text = newLocation.description() +
getItemsDescription(newLocation) +
getMobsDescription(newLocation) +
getCanMoveDirectionsDescription(newLocation);
updateScreen();
}
else
{
informInvalidGoDirection("south");
}
}
}
private class EastButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
Location currentLocation = Application.instance().playerCurrentLocation();
boolean isLocationValid = Application.instance().isThereALocationEastOf(currentLocation);
if(isLocationValid)
{
Application.instance().movePlayerEast();
Location newLocation = Application.instance().playerCurrentLocation();
text = newLocation.description() +
getItemsDescription(newLocation) +
getMobsDescription(newLocation) +
getCanMoveDirectionsDescription(newLocation);
updateScreen();
}
else
{
informInvalidGoDirection("east");
}
}
}
private class WestButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
Location currentLocation = Application.instance().playerCurrentLocation();
boolean isLocationValid = Application.instance().isThereALocationWestOf(currentLocation);
if(isLocationValid)
{
Application.instance().movePlayerWest();
Location newLocation = Application.instance().playerCurrentLocation();
text = newLocation.description() +
getItemsDescription(newLocation) +
getMobsDescription(newLocation) +
getCanMoveDirectionsDescription(newLocation);
updateScreen();
}
else
{
informInvalidGoDirection("west");
}
}
}
private static class GuiWindowListener extends WindowAdapter
{
@Override
public void windowClosing(WindowEvent ev)
{
ev.getComponent().setVisible(false);
System.exit(0);
} /* end windowClosing */
} /* end GuiWindowListener */
private static final int FRAME_HEIGHT = 480;
private static final int FRAME_WIDTH = 640;
private static JButton northButton;
private static JButton southButton;
private static JButton eastButton;
private static JButton westButton;
// private static JLabel label;
private static String text;
private static JPanel mainPanel;
private static JScrollPane scroller;
private static JFrame frame;
private static JTextArea textArea;
}
这是简化版本,以强调我遇到的问题 注意 - &gt;滚动问题发生,直到下次发生screenUpdate()
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class GraphicViewSimplified {
public static void main(String[] args)
{
GraphicViewSimplified view = new GraphicViewSimplified();
}
GraphicViewSimplified()
{
frame = new JFrame();
frame.addWindowListener(new GuiWindowListener());
frame.setLocation(100, 200);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Nittany Cubs Gaming GUI");
frame.setLayout(new BorderLayout());
northButton = new JButton("Go North");
// northButton.addActionListener(new NorthButtonActionListener());
frame.add(northButton, BorderLayout.NORTH);
eastButton = new JButton("Go east");
// eastButton.addActionListener(new EastButtonActionListener());
frame.add(eastButton, BorderLayout.EAST);
southButton = new JButton("Go south");
// southButton.addActionListener(new SouthButtonActionListener());
frame.add(southButton, BorderLayout.SOUTH);
westButton = new JButton("Go west");
// westButton.addActionListener(new WestButtonActionListener());
frame.add(westButton, BorderLayout.WEST);
text = ";lakjd;lfkjasd;lfkjas;ldfkjas;lkdfj;a\naj;lsdkfjas;lkdjf\naal;skjfw;lk\n";
//mainPanel = new JPanel();
textArea = new JTextArea(text);
textArea.setEditable(false);
scroller = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroller,BorderLayout.CENTER);
while(true)
{
updateScreen();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void updateScreen()
{
String alph = "abcdefghijklmnopqrstuvwxyz";
int firstInt = (int)(Math.random()*100);
int secondInt = (int)(Math.random()*100);
for(int i = 0; i < firstInt ; i++)
{
text += alph.charAt((int)(Math.random()*alph.length()));
}
text += "\n";
for(int i = 0; i < secondInt ; i++)
{
text += alph.charAt((int)(Math.random()*alph.length()));
}
text += "\n";
// JTextArea ta = new JTextArea(text);
textArea = new JTextArea(text);
textArea.setEditable(false);
//mainPanel.add(textArea);
scroller = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//scroller = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroller,BorderLayout.CENTER);
frame.setVisible(true);
}
private static class GuiWindowListener extends WindowAdapter
{
@Override
public void windowClosing(WindowEvent ev)
{
ev.getComponent().setVisible(false);
System.exit(0);
} /* end windowClosing */
} /* end GuiWindowListener */
private static final int FRAME_HEIGHT = 480;
private static final int FRAME_WIDTH = 640;
private static JButton northButton;
private static JButton southButton;
private static JButton eastButton;
private static JButton westButton;
private static String text;
// private static JPanel mainPanel;
private static JScrollPane scroller;
private static JFrame frame;
private static JTextArea textArea;
}
答案 0 :(得分:1)
首先你在这里阻止EDT
,这是不对的:
while (true) {
updateScreen();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
为此目的,您可以使用Swing Timer
,它可以用于按间隔更新,也可以用1500或更大的间隔,因为在调整帧大小时会出现滞后。
Timer t = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
updateScreen();
}
});
t.start();
至于我,你似乎真的需要SwingWorker
。 Here就是很好的例子。
答案 1 :(得分:0)
好的,我想出了我自己的问题的答案我需要添加remove(OldComponents)revalidate()然后在updateScreen()函数中添加(newComponent)然后revalidate()。我搞定了!感谢您的帮助