似乎不可能修复Graphics2D缺少的代码,我必须设置动画的修复大小,否则一些开始外面的矩形490 x 490冻结或在屏幕上摇晃
我的SSCCE
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.table.*;
public class OneTwoThree implements Runnable {
private String testS;
private JFrame frame = new JFrame("frameTitle");
private JScrollPane tblS = new JScrollPane();
private JTable tbl;
private Vector<String> rOrH;
private long t1 = 0L;
private long t2 = 0L;
private AlphaChars aChars = new AlphaChars();
private GradientViewPortOriginal tableViewPort;
private JViewport viewport = new JViewport();
private Random random …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个Java应用程序,它在屏幕上绘制多个球,从框架的边缘反弹.我可以成功地画出一个球.但是当我添加第二个球时,它会覆盖我绘制的初始球.代码是:
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
public class Ball extends JPanel implements Runnable {
List<Ball> balls = new ArrayList<Ball>();
Color color;
int diameter;
long delay;
private int x;
private int y;
private int vx;
private int vy;
public Ball(String ballcolor, int xvelocity, int yvelocity) {
if(ballcolor == "red") {
color = Color.red;
}
else if(ballcolor == "blue") {
color = Color.blue;
}
else if(ballcolor == "black") {
color = Color.black;
}
else if(ballcolor == "cyan") {
color …Run Code Online (Sandbox Code Playgroud) 我希望根据鼠标事件(鼠标进入,退出,按下等)获得自定义颜色.所以要做到这一点,我写了下面的代码.它对一切都很好,除了鼠标按下事件的情况,什么都不做.它只有当我覆盖在色彩的作品UIManager是这样UIManager.put("Button.select", Color.red);.问题UIManager是,它会改变我的所有按钮.
任何人都可以告诉我,我可能做错了什么,或者最好的方法是什么,我想要做什么?
我的代码:
final JButton btnSave = new JButton("Save");
btnSave.setForeground(new Color(0, 135, 200).brighter());
btnSave.setHorizontalTextPosition(SwingConstants.CENTER);
btnSave.setBorder(null);
btnSave.setBackground(new Color(3, 59, 90));
btnSave.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
btnSave.setBackground(new Color(3, 59, 90));
}
@Override
public void mousePressed(MouseEvent e) {
// Not working :(
btnSave.setBackground(Color.pink);
}
@Override
public void mouseExited(MouseEvent e) {
btnSave.setBackground(new Color(3, 59, 90));
}
@Override
public void mouseEntered(MouseEvent e) {
btnSave.setBackground(new Color(3, 59, 90).brighter());
}
@Override
public void mouseClicked(MouseEvent e) { …Run Code Online (Sandbox Code Playgroud) 我正在为我的设计使用MVC模式,当用户按下搜索按钮时,我在模型中调用搜索,但我还想更新从该模型返回的信息的进度条.
我尝试过使用swingworker,但进度条没有更新.我怀疑我的线程出错了.
我在控制器中定义的按钮是:
class SearchBtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
_view.displayProgress();
}
}
Run Code Online (Sandbox Code Playgroud)
这将调用模型中的搜索,并在视图中进行以下调用:
public void displayProgress() {
TwoWorker task = new TwoWorker();
task.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if ("progress".equals(e.getPropertyName())) {
_progressBar.setValue((Integer) e.getNewValue());
}
}
});
task.execute();
}
private class TwoWorker extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
_model.startSearch(getTerm()); // time intensive code
File file = new File("lock");
while (file.exists()){
setProgress(_model.getStatus());
System.out.println(_model.getStatus()); // never called
}
return null;
} …Run Code Online (Sandbox Code Playgroud) 我做了一个简单的Swing应用程序.但渲染表现出错误.我做错了什么或者它是一个错误?
这是一个简单的小型JFrame,带有文本字段,按钮和空列表.如果我首先水平调整窗口大小然后键入文本字段,按钮会突然消失.
这是我的代码:
public class App extends JFrame {
public App() {
JTextField messageFld = new JTextField();
JButton saveBtn = new JButton("Save");
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(messageFld, BorderLayout.CENTER);
inputPanel.add(saveBtn, BorderLayout.EAST);
JList<Data> list = new JList<Data>();
JPanel panel = new JPanel(new BorderLayout());
panel.add(inputPanel, BorderLayout.NORTH);
panel.add(list, BorderLayout.CENTER);
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Test application");
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new App();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
以下是一些截图:
在启动时

水平调整大小后

在文本字段中列出了几个字符

将鼠标移到按钮上后

我使用Windows 7,Java …
我正在使用NET Beans IDE在LINUX中开发我的应用程序.我使用synthetica包来产生新的外观和感觉.一直都很好.
现在我的下一个阶段是在某些数据库状态发生变化时为按钮添加颜色.
例如:
在一家餐馆,我有2张桌子,当有8个人进来用餐时,我将在我的软件中创建2个桌子,因为人们无人看管我希望这两个桌子的按钮是绿色的.处理任何这些表的订单时,处理表的按钮颜色应更改为橙色.处理时,它应该是闪烁的颜色.如何在java中执行此操作?我将负责数据库更新我只想知道如何更改按钮的颜色和添加闪烁技术.
我想弄清楚如何为摆动组件设置动画,使其从 a 点移动到 b 点。这是一个让红色 JPanel 从左向右移动的代码示例:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MovingSquareExample {
private static final JPanel square = new JPanel();
private static int x = 20;
public static void createAndShowGUI(){
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(square);
square.setBounds(20,200,100,100);
square.setBackground(Color.RED);
Timer timer = new Timer(1000/60,new MyActionListener());
timer.start();
frame.setVisible(true);
}
public static class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
square.setLocation(x++, 200);
}
}
public static void …Run Code Online (Sandbox Code Playgroud) 我正在做一些练习来理解Java和Swing API.为什么在Disegno构造函数中有nullPointerException?我想打印两个矩形的坐标,但它们似乎没有初始化.
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Disegno extends JFrame{
Disegno(){
this.setSize(500, 500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
MyPanel aba = new MyPanel();
this.setContentPane(aba);
this.setVisible(true);
System.out.println(aba.rect.blue.x + "-" + aba.rect.blue.y);
System.out.println(aba.rect.yellow.x + "-" + aba.rect.yellow.y);
}
public static void main(String[] args){
new Disegno();
}
}
class MyPanel extends JPanel{
JPanel up, down;
RectArea rect;
MyPanel(){
this.setLayout(new BorderLayout());
up = new JPanel();
this.add(up, BorderLayout.NORTH);
up.setBackground(Color.red);
up.setVisible(true);
down = new JPanel();
down.setBackground(Color.green);
this.add(down, BorderLayout.SOUTH);
down.setVisible(true);
rect = new RectArea();
this.add(rect, BorderLayout.CENTER);
this.setVisible(true);
} …Run Code Online (Sandbox Code Playgroud) 在我提出我的问题之前,我对任何不一致表示歉意.我是相当新的.我正在制作一款现在看起来像这样的游戏(图片并不重要):
红点应该向右移动,他们用计时器做到这一点.这很好用.图形不会更新,所以我必须来回拖动窗口的一侧,看看我的点在移动.我该怎么做才能解决这个问题?
我的mainclass中的paintcomponent方法:
public void paintComponent(Graphics g){
super.paintComponent(g);
for (int x = 0; x < SomeInts.amount; x++){
for (int y = 0; y < SomeInts.amount; y++){
tile[x][y].colorBody(g, x, y);
Tower temp;
for (int i = 0; i < towers.size(); i++){
temp = towers.get(i);
temp.colorBody(g, tile[x][y].getSize());
temp.guard.colorBody(g, tile[x][y].getSize());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的红点课.也称为Guard类:
public class Guard {
int x, y, size = 10, towerx, towery;
Timer timer;
public Guard(int towerx1, int towery1){
towerx = towerx1;
towery = towery1;
x = towerx …Run Code Online (Sandbox Code Playgroud) 我这里有这个代码用于创建像素数组并将其绘制到图像中:
import javax.swing.JFrame;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
public class test extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int WIDTH = 800;
public static int HEIGHT = 600;
public boolean running = true;
public int[] pixels;
public BufferedImage img;
public static JFrame frame;
private Thread thread;
public static void main(String[] arg) {
test wind = new test();
frame = new JFrame("WINDOW");
frame.add(wind);
frame.setVisible(true);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wind.init();
} …Run Code Online (Sandbox Code Playgroud) java ×10
swing ×10
animation ×1
awt ×1
components ×1
graphics ×1
graphics2d ×1
java-7 ×1
jbutton ×1
jframe ×1
jviewport ×1
mouseevent ×1
netbeans ×1
swingworker ×1
timer ×1