我试图在JFrame窗口中用鼠标画线,当我尝试这样做时,它不起作用!请忽略菜单,我想稍后再做一些事情!我错过了什么!如果你能给我一些提示,我将不胜感激!
public class newGUI extends JFrame implements ActionListener, MouseMotionListener, MouseListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
Point point1;
Point point2;
Line2D line2d;
public static void main(String[] args){
newGUI gui = new newGUI();
gui.setVisible(true);
}
public newGUI()
{
super("Menu Demonstration");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenu colorMenu = new JMenu("Choose Colors");
JMenuItem greenChoice = new JMenuItem("GREEN");
greenChoice.addActionListener(this);
colorMenu.add(greenChoice);
JMenuItem redChoice = new JMenuItem("RED");
colorMenu.add(redChoice);
JMenuBar …Run Code Online (Sandbox Code Playgroud) 我正在尝试用Java(学校项目)制作游戏,我有以下设置:
一个主要的类,用JFrame扩展,一个'Game'类,用JPanel扩展.
现在从这个主类中,我调用了一个类'Player'和一个类'Map'.类'Map'存在两个子类'Blocks'和'Bombs'.
但我想知道..我如何让所有这些类的绘制方法绘制到相同的JPanel(类Game)?
我给每个类的方法'public void paint(Graphics g)'并做了绘画..但是当我运行程序时,只有"Game"类的绘画出现,而不是来自子类的绘画.
我该如何实现?
例如,我将代码缩减为:
主要课程:
BomberGame game = new BomberGame();
add(game);
setSize(400, 400);
setTitle("Bomber");
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.show();
}
public static void main(String[] args) {
BomberB1 main = new BomberB1();
}
}
Run Code Online (Sandbox Code Playgroud)
游戏类:
package bomberb1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class BomberGame extends JPanel {
public BomberGame() {;
BomberMap map = new …Run Code Online (Sandbox Code Playgroud) 我正在尝试JMenu在点击它时动态构建一个动态(我只得到一个空菜单),下面是我的代码.
final JMenu JMWindows = new JMenu("Opened Windows");
JMWindows.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(JInternalFrame ji : desktop.getAllFrames())
{
JMWindows.add(ji.getTitle());
}
}
});
Run Code Online (Sandbox Code Playgroud)
我已经意识到它actionperformed永远不会被召唤,JMenu它就在里面JMenuBar.可能是什么问题呢 ?
我创建了一个测试版本来简化我为你们提出的问题:
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel l = new JLabel("hello");
JPanel cp;
public Test(){
setContentPane(cp = new JPanel());
cp.add(l);
setSize(200, 200);
setVisible(true);
}
public void paint(Graphics g){
//do absolutely nothing
}
}
Run Code Online (Sandbox Code Playgroud)
当你启动程序时,你会看到一个完美的空白窗口.但是,如果删除paint方法,JLabel会显示出来!(我不得不长时间搜索这个bug,真的).现在,我如何启用一个窗口来使用图形和定期绘制组件?提前致谢
我有一个程序,可以为JPanel(in JScrollbar)添加一堆组件.但是,由于它添加了很多组件,因此大多数组件都不适合可见区域(Viewport).
当一切都加载并且我开始向下滚动时,我注意到组件进入该Viewport区域时,正在对齐并设置它们的位置.这导致我JScrollPane高于必要.这使得它在我到达终点时"快速"(组件突然向上移动(正确对齐),视口也是如此).
我试着打电话repaint()和validate(),但没有任何效果.我究竟做错了什么?
我正在尝试创建一个JScrollPane包含JPanel高度增加和减少的数据.当它变得大于它的大小时JScrollPane,它应该创建一个垂直滚动条,这将允许我滚动整个JPanel.但是,我很难实现这一目标.是的,我知道我没有使用LayoutManagers.不,我不会使用它们,我需要一个不涉及其使用的解决方案.
这是两个按钮的AbstractAction添加和减去JPanel:
class AddACT extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
info.setSize(420,info.getHeight() + 40);
info.add(new SubPanel); // Adds another JPanel into the main JPanel (for content input)
gui.repaint();
infoS.validate();
}
}
class RemoveACT extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
info.remove(subPanel()); // This would remove the last JPanel added to the main JPanel
info.setSize(420,info.getHeight() - 40);
gui.repaint();
infoS.validate();
}
Run Code Online (Sandbox Code Playgroud)
以下是主要JPanel和JScrollPane的代码:
final JPanel info = …Run Code Online (Sandbox Code Playgroud) 我需要在java中使用一个小代码来实现以下场景:
按钮应该获取所选复选框并执行表单中的复选框的代码.
我最初是从 Chillax 开始的,在临近截止日期遇到这么多问题之后,我又回到了我更熟悉的 IDE NetBeans,并将我的方法更改为更基本的“Asteroid”类型游戏:
在 NetBeans 中,我得到:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at gayme.Craft.<init>(Craft.java:27)
at gayme.Board.<init>(Board.java:54)
at gayme.Gayme.<init>(Gayme.java:9)
at gayme.Gayme.main(Gayme.java:19)
Java Result: 1
Run Code Online (Sandbox Code Playgroud)
资料来源:(工艺 26 - 34)
public Craft() {
ImageIcon ii = new ImageIcon(this.getClass().getResource("craft.png"));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
missiles = new ArrayList();
visible = true;
x = 40;
y = 60;}
Run Code Online (Sandbox Code Playgroud)
(第 54 版)
craft = new Craft();
Run Code Online (Sandbox Code Playgroud)
(同性恋 9)
add(new Board());
Run Code Online (Sandbox Code Playgroud)
(同性恋 …