[英]how to adjust image in a panel in Java swing?
我有一些代碼,在SO的其他程序員的大力幫助下。 首先,謝謝大家。 現在我有了適用於hangman的代碼。 我希望文本框出現在圖像下方,現在它正在其上方拍攝。 我在代碼中提供了圖像的鏈接。 但是您可能需要下載它以避免引發異常。 我想為計時器保留右上角的空閑時間,但我還沒有想到該怎么做。 我需要幫助以將圖像和texfield放在正確的位置。 請執行代碼以查看當前外觀。 我同時嘗試了BorderLayout.SOUTH和BorderLayout.PAGE_END,但沒有幫助,謝謝
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
public class HangmanGUI {
private DetailsPanel myPanel;
private ImagePanel imagePanel = new ImagePanel();
public HangmanGUI() throws ParseException {
myPanel = new DetailsPanel();
JFrame myframe = new JFrame();
// myframe.getContentPane().setLayout(new BorderLayout());
myframe.getContentPane().add(imagePanel, BorderLayout.CENTER);
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
myframe.setTitle("Hangman Game");
// myframe.setVisible(true);
// myframe.setLocationRelativeTo(null);
myframe.pack();
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setLocationRelativeTo(null);
myframe.setVisible(true);
}
public static void main(String[] args) throws ParseException {
new HangmanGUI();
}
}
class ImagePanel extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final String TITLE = "Hangman Image";
private BufferedImage image;
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public ImagePanel() {
setBorder(BorderFactory.createTitledBorder(TITLE));
try {
image = ImageIO.read(new File("http://upload.wikimedia.org/wikipedia/commons/8/8b/Hangman-0.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
add(createFormattedPanel(),BorderLayout.SOUTH);
//add(createFormattedPanel(),BorderLayout.PAGE_END);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
}
public JPanel createFormattedPanel() {
JPanel panel = new JPanel();
MaskFormatter formatter = null;
try {
JLabel label = new JLabel("Guesss");
formatter = new MaskFormatter("? ? ? ? ? ? ?");
formatter.setPlaceholderCharacter('?');
JFormattedTextField input = new JFormattedTextField(formatter);
input.setColumns(20);
panel.add(label);
panel.add(input);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return panel;
}
}
class DetailsPanel extends JPanel {
public DetailsPanel() {
setLayout(new BorderLayout());
setBorder(BorderFactory.createTitledBorder(" click here "));
//add(createFormattedPanel(), BorderLayout.PAGE_START);
JPanel letterPanel = new JPanel(new GridLayout(0, 5));
for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
String buttonText = String.valueOf(alphabet);
JButton letterButton = new JButton(buttonText);
letterButton.addActionListener(clickedbutton());
letterPanel.add(letterButton, BorderLayout.CENTER);
}
add(letterPanel, BorderLayout.CENTER);
}
private ActionListener clickedbutton() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
System.out.println("actionCommand is: " + actionCommand);
}
};
}
}
因為您要將自定義繪畫和組件混合到單個組件中,所以您無法控制組件的布局方式。
我想到兩種解決方案...
使用JLabel
顯示圖像,將其放置在ImagePane
的CENTER
位置,將“格式化的面板”放置在SOUTH
位置
拆分您的UI,以便隔離每個組件...
-- Hangman Image --------------
| --------------------------- |
| | ----------------------- | |
| | | | | |
| | | | | |
| | | (Image Pane) | | |
| | | | | |
| | | | | |
| | ----------------------- | |
| | ----------------------- | |
| | | Guess: ? ? ? ? ? ? | | |
| | ----------------------- | |
| --------------------------- |
| -- click here ------------- |
| | | |
| | | |
| | (buttons ) | |
| | | |
| | | |
| --------------------------- |
-------------------------------
基本上, ImagePane
, GuessPane
和ClickPane
都是獨立的組件。
然后,您可以將ImagePane
和GuessPane
添加到另一個JPanel
(使用BorderLayout
),然后將其和ClickPane
添加到主面板上(使用BorderLayout
)。
這將使您更好地控制各個組件的布局和組合方式。
==使用JLabel
示例==
這是使用JLabel
不是自定義繪畫的基本示例。
ImagePane
和GuessPane
添加到單獨的面板,該面板添加到框架的CENTER
位置。 然后將DetailsPane
添加到框架的SOUTH
位置...
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class HangmanGUI {
private DetailsPanel myPanel;
public HangmanGUI() throws ParseException {
myPanel = new DetailsPanel();
JFrame myframe = new JFrame();
JPanel content = new JPanel(new BorderLayout());
content.add(new ImagePane());
content.add(new GuessPane(), BorderLayout.SOUTH);
content.setBorder(BorderFactory.createTitledBorder("Hangman Image"));
myframe.getContentPane().add(content, BorderLayout.CENTER);
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
myframe.setTitle("Hangman Game");
myframe.pack();
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setLocationRelativeTo(null);
myframe.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
try {
new HangmanGUI();
} catch (ParseException ex) {
ex.printStackTrace();
}
}
});
}
public class ImagePane extends JPanel {
private JLabel label;
public ImagePane() {
setLayout(new BorderLayout());
label = new JLabel();
add(label);
try {
label.setIcon(new ImageIcon(ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/commons/8/8b/Hangman-0.png"))));
} catch (IOException ex) {
label.setText("Bad Image");
ex.printStackTrace();
}
}
}
public static class GuessPane extends JPanel {
public GuessPane() {
MaskFormatter formatter = null;
try {
JLabel label = new JLabel("Guesss");
formatter = new MaskFormatter("? ? ? ? ? ? ?");
formatter.setPlaceholderCharacter('?');
JFormattedTextField input = new JFormattedTextField(formatter);
input.setColumns(20);
add(label);
add(input);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
}
}
public static class DetailsPanel extends JPanel {
public DetailsPanel() {
setLayout(new BorderLayout());
setBorder(BorderFactory.createTitledBorder(" click here "));
//add(createFormattedPanel(), BorderLayout.PAGE_START);
JPanel letterPanel = new JPanel(new GridLayout(0, 5));
for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
String buttonText = String.valueOf(alphabet);
JButton letterButton = new JButton(buttonText);
letterButton.addActionListener(clickedbutton());
letterPanel.add(letterButton, BorderLayout.CENTER);
}
add(letterPanel, BorderLayout.CENTER);
}
private ActionListener clickedbutton() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
System.out.println("actionCommand is: " + actionCommand);
}
};
}
}
}
這種類型的分離將需要使用某種模型來跨越組件。
也就是說,當用戶做出猜測時,您需要更新模型,這將通知其他組件(通過某種偵聽器API)發生了某些更改,這將允許其他UI元素根據需要進行更新。符合模型的要求。
該模型應包含游戲邏輯並驅動UI。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.