如何在单个框架中显示 HTML 内容和密码字段?
How to display HTML content and password field in a single frame?
我在 JScrollPane
内的 JEditorPane
中显示了一个 HTML 页面。我需要在同一个 window 中创建一个 JPasswordField
来获取用户的密码。
我该如何实现?
How can I implement that?
很多不同的方式。这是一个。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
public class PswdHtml implements Runnable {
@Override
public void run() {
createAndShowGui();
}
private void createAndShowGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createPane());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JScrollPane createPane() {
try {
JEditorPane editPane = new JEditorPane("https://www.google.com");
JScrollPane scrollPane = new JScrollPane(editPane);
scrollPane.setColumnHeaderView(createPassword());
scrollPane.setPreferredSize(new Dimension(450, 300));
return scrollPane;
}
catch (IOException x) {
throw new RuntimeException(x);
}
}
private JPanel createPassword() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Password");
panel.add(label);
JPasswordField passwordField = new JPasswordField(10);
panel.add(passwordField);
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(new PswdHtml());
}
}
参考 Creating a GUI With Swing,它是 Oracle Java 教程中的 trails 之一。
我在 JScrollPane
内的 JEditorPane
中显示了一个 HTML 页面。我需要在同一个 window 中创建一个 JPasswordField
来获取用户的密码。
我该如何实现?
How can I implement that?
很多不同的方式。这是一个。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
public class PswdHtml implements Runnable {
@Override
public void run() {
createAndShowGui();
}
private void createAndShowGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createPane());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JScrollPane createPane() {
try {
JEditorPane editPane = new JEditorPane("https://www.google.com");
JScrollPane scrollPane = new JScrollPane(editPane);
scrollPane.setColumnHeaderView(createPassword());
scrollPane.setPreferredSize(new Dimension(450, 300));
return scrollPane;
}
catch (IOException x) {
throw new RuntimeException(x);
}
}
private JPanel createPassword() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Password");
panel.add(label);
JPasswordField passwordField = new JPasswordField(10);
panel.add(passwordField);
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(new PswdHtml());
}
}
参考 Creating a GUI With Swing,它是 Oracle Java 教程中的 trails 之一。