我已经创建了一个蛇和梯子游戏,它运行良好,但现在我想添加一点修改它.我希望每个球员都有两个球员而不是一个球员.现在新的规则变成了,同一个玩家的两个部分可以占据同一个方格,因此我需要一种方法来在同一个方格中显示它们.我使用JLable来识别每个方块,但现在我希望每个方块都能容纳多个方块.也就是说,如果一个正方形被多个部分占用,则用户必须能够点击一个部分并仅选择该部分.有没有办法将square/JLable分成几个正方形/ JLabel?我不确定JLabel是否最好在这里使用...有什么建议吗???
我使用Netbeans的GUI Builder为我的应用程序创建了GUI.我试图显示一个JFrame包含JLabel图像的图像,我无法Image显示.
我生成的代码:
private void initComponents() {
//...
jLabel1 = new JLabel(new ImageIcon(myPicture));
}
Run Code Online (Sandbox Code Playgroud)
我的班级代码:
public class GUIWindow extends javax.swing.JFrame {
BufferedImage myPicture;
/** Creates new form GUIWindow */
public GUIWindow() throws IOException {
myPicture = ImageIO.read(new File("images/logo.png"));
initComponents();
this.add(jLabel1);
}
}
Run Code Online (Sandbox Code Playgroud)
但我仍然没有看到图像...... (图像文件的路径很好)它的样子:
my-project :
/build
/dist
/images/logo.png
/nbproject
/src (here I have all my source files)
/build.xml
/manifest.mf
Run Code Online (Sandbox Code Playgroud) 对于我的任务,我得到了这段代码:
// This class/method uses a global variable that MUST be set before calling/using
// note: You can not call the paint routine directly, it is called when frame/window is shown
// look up the repaint() routine in the book
// Review Listings 8.5 and 8.6
//
public static class MyPanel extends JPanel {
public void paintComponent (Graphics g) {
int xpos,ypos;
super.paintComponent(g);
// set the xpos and ypos before you display the image
xpos = 10; // you pick …Run Code Online (Sandbox Code Playgroud) 我有一个JPanel,其中包含2个JPanel.位于左侧(左侧)和右侧(rB),我想在右侧JPanel(rB)上添加背景图像.
但我得到的结果是
我想要的结果
public void paintComponent(Graphics g)
{
//this.paintComponent(g);
if(wdimage != null) g.drawImage(wdimage,0,0,800,800,rB); //(image,location x, location y, size x, size y)
}
Run Code Online (Sandbox Code Playgroud)
rB Panel正在阻止图像,我想要的是在JPanel上显示图像,并在JPanel上添加一些jlabels和文本字段,稍后再添加图像.
我有一个绘图程序,允许用户绘制线条,框,文本,甚至JPEG.
我希望能够保存用户绘制的图像,但是我对如何将用户的创建转换为我可以轻松使用的格式(Image或BufferedImage)感到有点困惑.
用户在JPanel上绘制他们的内容(让我们称之为JPanel inputPanel).如果用户单击一个按钮(让我们调用此按钮saveButton).弹出一个JFileChooser,询问将其保存到哪里,然后BAM,保存创建(我知道如何以编程方式保存图像).
有没有一种简单的方法可以以这种方式将JPanel转换或转换为Image或BufferedImage?
谷歌搜索/搜索StackOverFlow仅提供使用setIcon()将图像绘制到JPanel的解决方案,这没有帮助.