我正在尝试创建一个打开指定网址的桌面应用程序。
我正在使用Java构建应用程序,到目前为止,这是我的代码。
browseropen.java
public class browseropen {
public static void main(String[] args) {
JFrame frame = new JFrame("Desktop Browser Search"); // We create our initial app frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // next we are making sure our app stops running
JPanel panel = new JPanel(); // Our new application window
JLabel optext = new JLabel("Welcome Please Enter URL to Begin!"); // Our new window text
frame.setSize(400, 400); // Set Application Window Size
//pack();
// Now adding Text box
JTextField txtbox = new JTextField(10);
JButton gourl = new JButton("Go To URL"); // creating new button to activate browser function
frame.setVisible(true);
optext.setVisible(true);
frame.setLayout( new GridLayout(3,4,5,10));
panel.add(optext); // We are adding our objects to our app window
panel.add(txtbox);
panel.add(gourl);
frame.add(panel); // And finally we add our app panel to our frame
String geturl = txtbox.getText();
gourl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//run client main
runClient();
}
public void runClient() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
String[] args1={"10"};
openbrowserurl.main(args1);
}
});
}
});
}
public void texturl() {
};
}
openbrowser.java
public class openbrowserurl extends browseropen {
public static void main(String[] args) {
Desktop desktop = Desktop.getDesktop();
String urlinput = "https://www.google.com";
if( !java.awt.Desktop.isDesktopSupported() ) {
System.err.println("Desktop Not Supported");
System.exit(1);
}
if (args.length == 0) {
System.out.println( "Usage: OpenURI [URI [URI ... ]]" );
System.exit( 0 );
}
if(!desktop.isSupported( java.awt.Desktop.Action.BROWSE ) ) {
System.err.println( "Desktop doesn't support the browse action (fatal)" );
System.exit( 1 );
}
for ( String arg : args ) {
try {
java.net.URI uri = new java.net.URI( urlinput );
desktop.browse( uri );
}
catch ( Exception e ) {
System.err.println( e.getMessage() );
}
}
}
}
如果在openbrowser.java中指定了URL,我可以打开它,但我想读取我在browseropen.java中创建的JTextField
的值。
答案 0 :(得分:1)
您目前无法从browseropen类中的JTextField获取信息,因为它是在静态main方法中声明的,因此仅在该方法中可见。你的整个项目对我们很尖叫 - 他还不了解面向对象的编程或者Java如何实现它,因为这会妨碍你的尝试。
建议:
其他问题:
setBounds(...)
是件好事,但我对您正在尝试做的事情感到困惑。您将JFrame的布局设置为GridLayout,需要3行和4列(总共12个组件),然而您只需将一个组件添加到JFrame,即“panel”JPanel。我不确定你要做什么,这使得在这种情况下建议很困难。例如,你可以像这样创建一个类:
public class GetUrlPanel extends JPanel {
private static final String PROMPT = "Welcome Please Enter URL to Begin!";
private JTextField urlField = new JTextField(10);
private JButton goToUrlButton = new JButton("Go To URL");
public GetUrlPanel() {
add(new JLabel(PROMPT));
add(urlField);
add(goToUrlButton);
}
public String getUrlFieldText() {
return urlField.getText();
}
public void addGoToUrlListener(ActionListener listener) {
goToUrlButton.addActionListener(listener);
}
}
允许外部类在需要的地方显示这个JPanel,允许他们决定按下按钮时要做什么,并允许他们提取JTextField的内容。
另一种选择是从JOptionPane更简单地获取URL,例如:
public static void main(String[] args) {
if (!Desktop.isDesktopSupported()) {
System.err.println("Desktop is not supported. Exiting");
System.exit(1);
}
String message = "Welcome Please Enter URL to Begin!";
String title = "Enter URL";
int messageType = JOptionPane.PLAIN_MESSAGE;
String url = JOptionPane.showInputDialog(null, message, title, messageType);
if (url == null) {
System.err.println("Cancelled by user");
System.exit(0);
} else if (url.trim().isEmpty()) {
System.err.println("You must enter a URL. Exiting");
System.exit(2);
}
URI uri = null;
try {
uri = new URI(url);
Desktop desktop = Desktop.getDesktop();
desktop.browse(uri);
} catch (URISyntaxException | IOException e) {
String text = "Invalid URL \"" + url + "\". Exiting";
System.err.println(text);
e.printStackTrace();
}
}
有很多方法可以让这只猫受到伤害,但如果在回答这个问题时我想强调一件事,那就是: