在Java小程序中使用带图形的按钮
我有一个小程序,它使用drawLine()绘制网格,然后允许用户通过单击网格来放置和删除.png图像。现在我想在绘图空间的右侧添加一些界面按钮,如何才能做到这一点?目前,一个按钮拒绝移动到我想要的坐标,肯定有一个正确的方法来做到这一点在Java小程序中使用带图形的按钮,java,applet,Java,Applet,我有一个小程序,它使用drawLine()绘制网格,然后允许用户通过单击网格来放置和删除.png图像。现在我想在绘图空间的右侧添加一些界面按钮,如何才能做到这一点?目前,一个按钮拒绝移动到我想要的坐标,肯定有一个正确的方法来做到这一点 package test2; import java.applet.Applet; import java.awt.Button; import java.awt.Color; import java.awt.Graphics; import java.awt.
package test2;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
public class MainClass extends Applet implements MouseListener{
final int columnNumber = 10; //Number of columns
final int rowNumber = 8; //Number of rows
final int gridSize = 51;
private URL base;
//element images
private Image whiteSquare, resistanceHor, resistanceVer;
//creating a GridElement array
GridElement[][] gridElement = new GridElement[columnNumber][rowNumber];
public void init() {
setSize(columnNumber*gridSize + 100, rowNumber*gridSize);
//Here I try to add a button
Button resistanceButton = new Button("Button1");
resistanceButton.setLocation(columnNumber*gridSize, 20);
this.add(resistanceButton);
try {
base = getDocumentBase();
} catch (Exception e) {
// TODO: handle exception
}
resistanceHor = getImage(base, "data/resistance_hor.png");
resistanceVer = getImage(base, "data/resistance_ver.png");
whiteSquare = getImage(base, "data/white_square.png");
// Add the MouseListener to applet
addMouseListener(this);
for(int xcount=0; xcount<columnNumber;xcount++){
for(int ycount=0; ycount<rowNumber;ycount++){
//set top left corner coordinates for all grid elements and declare them empty and non-vertical
gridElement[xcount][ycount] = new GridElement(gridSize*xcount, gridSize*ycount,true,false);
}
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g){
//set grid color
Color gridGrey = new Color(208,195,195);
g.setColor(gridGrey);
//draw grid
for(int xcount=0; xcount<columnNumber;xcount++){
g.drawLine(xcount*gridSize, 0, xcount*gridSize, rowNumber*gridSize);
}
for(int ycount=0; ycount<rowNumber;ycount++){
g.drawLine(0, ycount*gridSize, columnNumber*gridSize, ycount*gridSize);
}
//draw png images in non-empty squares
for(int xcount=0; xcount<columnNumber;xcount++){
for(int ycount=0; ycount<rowNumber;ycount++){
if(gridElement[xcount][ycount].isEmpty() == false){
g.drawImage(resistanceHor, gridElement[xcount][ycount].getxTopLeftCorner(), gridElement[xcount][ycount].getyTopLeftCorner(), this);
}
else{
g.setColor( getBackground() );
g.fillRect(gridElement[xcount][ycount].getxTopLeftCorner()+1, gridElement[xcount][ycount].getyTopLeftCorner()+1, gridSize-1, gridSize-1);
}
}
}
}
public void mouseClicked (MouseEvent me) {
int col = me.getX() / gridSize; // Column where user clicked.
int row = me.getY() / gridSize; // Row where user clicked.
//if a square is empty, make it not empty, if it is not empty, make it empty
if(gridElement[col][row].isEmpty() == true){
gridElement[col][row].setEmpty(false);
}
else{
gridElement[col][row].setEmpty(true);
}
repaint();
}
public void mousePressed (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
public void mouseEntered (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
}
package test2;
public class GridElement {
private int xTopLeftCorner;
private int yTopLeftCorner;
private boolean vertical;
private boolean empty;
private boolean isResistor;
GridElement(int xTopLeftCorner, int yTopLeftCorner, boolean empty, boolean vertical){
this.xTopLeftCorner = xTopLeftCorner;
this.yTopLeftCorner = yTopLeftCorner;
this.empty = empty;
this.vertical = vertical;
}
public boolean isResistor() {
return isResistor;
}
public void setResistor(boolean isResistor) {
this.isResistor = isResistor;
}
public boolean isVertical() {
return vertical;
}
public boolean isEmpty() {
return empty;
}
public void setVertical(boolean vertical) {
this.vertical = vertical;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
public int getxTopLeftCorner() {
return xTopLeftCorner;
}
public int getyTopLeftCorner() {
return yTopLeftCorner;
}
public void setxTopLeftCorner(int xTopLeftCorner) {
this.xTopLeftCorner = xTopLeftCorner;
}
public void setyTopLeftCorner(int yTopLeftCorner) {
this.yTopLeftCorner = yTopLeftCorner;
}
包测试2;
导入java.applet.applet;
导入java.awt.Button;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.net.URL;
公共类MainClass扩展小程序实现MouseListener{
final int columnNumber=10;//列数
final int rowNumber=8;//行数
最终int gridSize=51;
私有URL基;
//元素图像
私有图像白色方块、resistanceHor、resistanceVer;
//创建GridElement数组
GridElement[][]GridElement=新的GridElement[columnNumber][rowNumber];
公共void init(){
设置大小(columnNumber*gridSize+100,rowNumber*gridSize);
//这里我尝试添加一个按钮
按钮电阻按钮=新按钮(“按钮1”);
resistanceButton.setLocation(columnNumber*gridSize,20);
此.add(电阻按钮);
试一试{
base=getDocumentBase();
}捕获(例外e){
//TODO:处理异常
}
resistanceHor=getImage(基本,“data/resistance_hor.png”);
resistanceVer=getImage(base,“data/resistance\u ver.png”);
whiteSquare=getImage(基,“data/white_square.png”);
//将鼠标侦听器添加到小程序
addMouseListener(这个);
对于(int xcount=0;xcount
基本上,顶级容器(如Applet、JApplet、Frame、JFrame、Dialog、JDialog等)的默认布局管理器为
对于具有BorderLayout的容器,组件将添加到BorderLayout.CENTER
位置。不仅如此,BorderLayout不考虑组件的首选尺寸,这意味着组件将拉伸以适合容器。在您的情况下,它将占据整个框架
解决方案
- 不要在小程序组件本身上绘制。请改为在
面板中绘制。将面板添加到小程序容器中。(中间正常)
- 创建另一个
并将按钮添加到该面板,然后将该面板添加到面板
,如BorderLayout.LINE\u END
添加(buttonPanel,BorderLayout.LINE\u END);
其他重要说明:
- 除非这是一个课堂作业,教授明确表示要使用AWT组件。不要使用Swing组件。Swing组件与AWT组件非常相似。它们通常在AWT组件前面加上
,例如J
。请参阅JPanel
- 不要像在
方法中那样显式调用update()
,而是调用paint()
repaint()
- 如果您不打算自己绘制背景,则应在
方法中调用paint
。如果不这样做,您将看到剩余的绘制工件super.paint(g)