Java 在新窗口中添加文本
我在这里遇到了一点情况。我正在制作一个新程序,当你点击菜单栏时,它会打开一个新的许可证窗口,现在问题是,我如何向新窗口添加文本,这是我的新窗口代码:Java 在新窗口中添加文本,java,netbeans,Java,Netbeans,我在这里遇到了一点情况。我正在制作一个新程序,当你点击菜单栏时,它会打开一个新的许可证窗口,现在问题是,我如何向新窗口添加文本,这是我的新窗口代码: JFrame frame = new JFrame("Licence"); frame.setSize(500,120); frame.setLocationRelativeTo(null); frame.setVisible(true); 我知道这是一个简单的问题,只是想不出正确的代码。您可以通过如下方式创建jlabel来添加文本
JFrame frame = new JFrame("Licence");
frame.setSize(500,120);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
我知道这是一个简单的问题,只是想不出正确的代码。您可以通过如下方式创建jlabel来添加文本:
JLabel label = new JLabel("Hello World");
然后可以将其添加到JFrame中。您可以通过如下方式创建JLabel来添加文本:
JLabel label = new JLabel("Hello World");
然后可以将其添加到JFrame中。您可以使用标签
JFrame frame = new JFrame("Licence");
JLabel label = new JLabel("Text-Only Label");
label.setFont(new Font("Serif", Font.PLAIN, 36));
frame.add(label);
您可以使用标签
JFrame frame = new JFrame("Licence");
JLabel label = new JLabel("Text-Only Label");
label.setFont(new Font("Serif", Font.PLAIN, 36));
frame.add(label);
你可以试试类似的东西
JDialog dialog = new JDialog(your_frame_reference, "Licence");
dialog .setModal(true);
dialog .setLocationRelativeTo(null);
dialog. getContentPane().add(new JLabel(your_text);
dialog .setVisible(true);
你可以试试类似的东西
JDialog dialog = new JDialog(your_frame_reference, "Licence");
dialog .setModal(true);
dialog .setLocationRelativeTo(null);
dialog. getContentPane().add(new JLabel(your_text);
dialog .setVisible(true);
试试这个例子
import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestDialog {
protected static void initUI() {
JPanel pane = newPane("Label in frame");
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
public static JPanel newPane(String labelText) {
JPanel pane = new JPanel(new BorderLayout());
pane.add(newLabel(labelText));
pane.add(newButton("Open dialog"), BorderLayout.SOUTH);
return pane;
}
private static JButton newButton(String label) {
final JButton button = new JButton(label);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Window parentWindow = SwingUtilities.windowForComponent(button);
JDialog dialog = new JDialog(parentWindow);
dialog.setLocationRelativeTo(button);
dialog.setModal(true);
dialog.add(newPane("Label in dialog"));
dialog.pack();
dialog.setVisible(true);
}
});
return button;
}
private static JLabel newLabel(String label) {
JLabel l = new JLabel(label);
l.setFont(l.getFont().deriveFont(24.0f));
return l;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initUI();
}
});
}
}
试试这个例子
import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestDialog {
protected static void initUI() {
JPanel pane = newPane("Label in frame");
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
public static JPanel newPane(String labelText) {
JPanel pane = new JPanel(new BorderLayout());
pane.add(newLabel(labelText));
pane.add(newButton("Open dialog"), BorderLayout.SOUTH);
return pane;
}
private static JButton newButton(String label) {
final JButton button = new JButton(label);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Window parentWindow = SwingUtilities.windowForComponent(button);
JDialog dialog = new JDialog(parentWindow);
dialog.setLocationRelativeTo(button);
dialog.setModal(true);
dialog.add(newPane("Label in dialog"));
dialog.pack();
dialog.setVisible(true);
}
});
return button;
}
private static JLabel newLabel(String label) {
JLabel l = new JLabel(label);
l.setFont(l.getFont().deriveFont(24.0f));
return l;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initUI();
}
});
}
}
还有一件事,我该如何使文本包装?检查这个问题,这将帮助您:很快您可以在字符串中使用html,它将自动扭曲它JLabel label=new JLabel(“你好,世界!诸如此类””);我不认为在jLabel中使用scroll是可能的,但是如果您想使用scroll,我认为您可以使用JTextArae。。。在这里你可以看到如何使用它。。。检查第一个答案还有一件事,我该如何使它这样的文本包装?检查这个问题,这将帮助你:很快你可以在你的字符串中使用html,它将自动扭曲它JLabel label=new JLabel(“Hello World!blah blah blah”);我不认为在jLabel中使用scroll是可能的,但是如果您想使用scroll,我认为您可以使用JTextArae。。。在这里你可以看到如何使用它。。。检查第一个答案