gpt4 book ai didi

java - 尝试将 JLabel 放置在另一个带有图像的 JLabel 上

转载 作者:行者123 更新时间:2023-12-01 13:00:34 25 4
gpt4 key购买 nike

我知道这个问题之前已经被问过。但我的却是独一无二的。我的编程有特定的顺序,并且我在使用带有文本的 JLabel 时遇到问题。我需要将文本保留在 JFrame 的中心,但也保留在图像上方。我尝试了多种选项,但似乎没有任何效果。相关编码如下:

public class SceneOne {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
JFrame SceneOne= new JFrame();
SceneOne.setSize(1400,700);
SceneOne.setTitle("The Car");
SceneOne.setVisible(true);
SceneOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SceneOne.setLocation(500,300);





ImageIcon image = new ImageIcon("/Users/computerscience2/Desktop/dark-forest-night-image.jpg");
JLabel imageLabel = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( imageLabel, BorderLayout.CENTER );
SceneOne.add(panel);
SceneOne.setResizable(true);
imageLabel.setVisible(true);
SceneOne.pack();


Button Leave=new Button("Leave");
Button Stay= new Button("Stay");
Leave.setVisible(true);
Stay.setVisible(true);
JPanel Leavebutton =new JPanel (new GridBagLayout());
Leavebutton.setVisible(true);
JPanel Staybutton=new JPanel (new GridBagLayout());
Staybutton.setVisible(true);
Leavebutton.add(Leave);
Staybutton.add(Stay);
FlowLayout two = new FlowLayout(FlowLayout.LEFT);
FlowLayout three=new FlowLayout(FlowLayout.RIGHT);
Leavebutton.setLayout(two);
Staybutton.setLayout(three);
SceneOne.add(Leavebutton);
SceneOne.add(Staybutton);


JLabel label1 = new JLabel("Test");
SceneOne.add(label1);


label1.setText("<html><font color='white'> It was approximately 11:30 pm. The night sky was black not a single star piercing through the darkness"
+ "except the thick and powerful moonlight." +"<br>" + "You are alone leaving a costume party at a friend's place."
+ "It was rather boring and you decided to leave early."+"A stutter is heard and your"+ "<br>"+ "car begins to shake"
+ "Your headlights and car lights crack. The engine is left dead silent." + "You are left in a total silence"
+ "and baked in merely the moonlight." +"<br>" + "There is a mere second of silence till a harsh chill ripes through the"
+ "car like a bullet through paper. You are left dumbfounded. What do you do?</font><html>");
label1.setHorizontalAlignment(JLabel.CENTER);
label1.setVerticalAlignment(JLabel.BOTTOM);
label1.setVisible(true);
}

}

提前谢谢您

最佳答案

您可以使用JLayeredPane对于这些类型的订购。它具有 moveToFront(Component)、moveToBack(Component) 和 setPosition 等方法,可用于在其层内重新定位组件。

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class SceneOne extends JPanel
implements ActionListener {

private JLayeredPane layeredPane;
private JLabel imageLabel;
int frameWidth,frameHeight;
Rectangle bounds;

public SceneOne() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

//Set the width of the frame here. It could be changes to the image width.
frameWidth = 1400;
frameHeight = 700;
bounds = new Rectangle(0, 0, frameWidth, frameHeight);

final ImageIcon bgImage = new ImageIcon("/Users/computerscience2/Desktop/dark-forest-night-image.jpg");
imageLabel = new JLabel(bgImage);
if (bgImage != null) {
imageLabel.setBounds(0, 0,
bgImage.getIconWidth(),
bgImage.getIconHeight());
}

//Create and set up the layered pane.
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(frameWidth, frameHeight));

//The components
JPanel buttonPanel = createButtonPanel();

JLabel textLabel = new JLabel("Test");
textLabel.setText("<html><font color='white'> It was approximately 11:30 pm. The night sky was black not a single star piercing through the darkness"
+ "except the thick and powerful moonlight."
+ "<br>"
+ "You are alone leaving a costume party at a friend's place."
+ "It was rather boring and you decided to leave early."
+ "A stutter is heard and your"
+ "<br>"
+ "car begins to shake"
+ "Your headlights and car lights crack. The engine is left dead silent."
+ "You are left in a total silence"
+ "and baked in merely the moonlight."
+ "<br>"
+ "There is a mere second of silence till a harsh chill ripes through the"
+ "car like a bullet through paper. You are left dumbfounded. What do you do?</font><html>");
textLabel.setHorizontalAlignment(JLabel.CENTER);
textLabel.setVerticalAlignment(JLabel.BOTTOM);
textLabel.setBounds(bounds);

//Adding components in order
layeredPane.add(imageLabel, new Integer(0));
layeredPane.add(buttonPanel, new Integer(1));
layeredPane.add(textLabel, new Integer(2));

add(layeredPane);
}

private JPanel createButtonPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setBounds(bounds);
panel.setOpaque(false);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

GridBagConstraints c = new GridBagConstraints();

JButton button;
panel.setLayout(new GridBagLayout());
c.fill = GridBagConstraints.NONE;

button = new JButton("Stay");
c.weightx = 0.5;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.FIRST_LINE_START;
button.addActionListener(this);
panel.add(button, c);

button = new JButton("Leave");
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
c.anchor = GridBagConstraints.FIRST_LINE_END;
button.addActionListener(this);
panel.add(button, c);

return panel;
}

public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();

if ("Stay".equalsIgnoreCase(cmd)) {
JOptionPane.showMessageDialog(this, "Stay here!!!");
} else if ("Leave".equalsIgnoreCase(cmd)) {
JOptionPane.showMessageDialog(this, "Bye bye!!!");
}
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
JFrame frame = new JFrame("SceneOne");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newContentPane = new SceneOne();
frame.setLayout(null);
frame.setContentPane(newContentPane);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

关于java - 尝试将 JLabel 放置在另一个带有图像的 JLabel 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23545692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com