当前位置: 技术问答>java相关
关于Applet请高手给点建议,跟帖有分!
来源: 互联网 发布时间:2015-10-25
本文导语: 本人刚刚接触java而且还是从applet开始,可是搞个界面就已经把我弄晕了。一会GridBagLayout,一会BorderLayout,结果我连一个web界面摆不出来。请高手建议一下哪本书比较适合,或者哪里有相应的代码可以下载,让我参考一...
本人刚刚接触java而且还是从applet开始,可是搞个界面就已经把我弄晕了。一会GridBagLayout,一会BorderLayout,结果我连一个web界面摆不出来。请高手建议一下哪本书比较适合,或者哪里有相应的代码可以下载,让我参考一下。谢了!
|
你试试这个:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.jnlp.*;
/**
A calculator with a calculation history that can be
deployed as a Java Web Start application.
*/
public class WebStartCalculator
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
A frame with a calculator panel and a menu to load and
save the calculator history.
*/
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle();
Container contentPane = getContentPane();
panel = new CalculatorPanel();
contentPane.add(panel);
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = fileMenu.add("Open");
openItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
open();
}
});
JMenuItem saveItem = fileMenu.add("Save");
saveItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
save();
}
});
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
pack();
}
/**
Gets the title from the persistent store or
asks the user for the title if there is no prior entry.
*/
public void setTitle()
{
try
{
String title = null;
BasicService basic = (BasicService)
ServiceManager.lookup("javax.jnlp.BasicService");
URL codeBase = basic.getCodeBase();
PersistenceService service = (PersistenceService)
ServiceManager.lookup(
"javax.jnlp.PersistenceService");
URL key = new URL(codeBase, "title");
try
{
FileContents contents = service.get(key);
InputStream in = contents.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
title = reader.readLine();
}
catch (FileNotFoundException exception)
{
title = JOptionPane.showInputDialog(
"Please supply a frame title:");
if (title == null) return;
service.create(key, 100);
FileContents contents = service.get(key);
OutputStream out
= contents.getOutputStream(true);
PrintStream printOut = new PrintStream(out);
printOut.print(title);
setTitle(title);
}
setTitle(title);
}
catch (UnavailableServiceException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (MalformedURLException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (IOException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
}
/**
Opens a history file and updates the display.
*/
public void open()
{
try
{
FileOpenService service = (FileOpenService)
ServiceManager.lookup(
"javax.jnlp.FileOpenService");
FileContents contents = service.openFileDialog(".",
new String[] { "txt" });
JOptionPane.showMessageDialog(this, contents.getName());
if (contents != null)
{
InputStream in = contents.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null)
{
panel.append(line);
panel.append("n");
}
}
}
catch (UnavailableServiceException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (IOException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
}
/**
Saves the calculator history to a file.
*/
public void save()
{
try
{
ByteArrayOutputStream out =
new ByteArrayOutputStream();
PrintStream printOut = new PrintStream(out);
printOut.print(panel.getText());
InputStream data = new ByteArrayInputStream(
out.toByteArray());
FileSaveService service = (FileSaveService)
ServiceManager.lookup(
"javax.jnlp.FileSaveService");
service.saveFileDialog(".",
new String[] { "txt" }, data, "calc.txt");
}
catch (UnavailableServiceException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (IOException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
}
private CalculatorPanel panel;
}
/**
A panel with calculator buttons and a result display.
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.jnlp.*;
/**
A calculator with a calculation history that can be
deployed as a Java Web Start application.
*/
public class WebStartCalculator
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
A frame with a calculator panel and a menu to load and
save the calculator history.
*/
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle();
Container contentPane = getContentPane();
panel = new CalculatorPanel();
contentPane.add(panel);
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = fileMenu.add("Open");
openItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
open();
}
});
JMenuItem saveItem = fileMenu.add("Save");
saveItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
save();
}
});
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
pack();
}
/**
Gets the title from the persistent store or
asks the user for the title if there is no prior entry.
*/
public void setTitle()
{
try
{
String title = null;
BasicService basic = (BasicService)
ServiceManager.lookup("javax.jnlp.BasicService");
URL codeBase = basic.getCodeBase();
PersistenceService service = (PersistenceService)
ServiceManager.lookup(
"javax.jnlp.PersistenceService");
URL key = new URL(codeBase, "title");
try
{
FileContents contents = service.get(key);
InputStream in = contents.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
title = reader.readLine();
}
catch (FileNotFoundException exception)
{
title = JOptionPane.showInputDialog(
"Please supply a frame title:");
if (title == null) return;
service.create(key, 100);
FileContents contents = service.get(key);
OutputStream out
= contents.getOutputStream(true);
PrintStream printOut = new PrintStream(out);
printOut.print(title);
setTitle(title);
}
setTitle(title);
}
catch (UnavailableServiceException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (MalformedURLException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (IOException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
}
/**
Opens a history file and updates the display.
*/
public void open()
{
try
{
FileOpenService service = (FileOpenService)
ServiceManager.lookup(
"javax.jnlp.FileOpenService");
FileContents contents = service.openFileDialog(".",
new String[] { "txt" });
JOptionPane.showMessageDialog(this, contents.getName());
if (contents != null)
{
InputStream in = contents.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null)
{
panel.append(line);
panel.append("n");
}
}
}
catch (UnavailableServiceException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (IOException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
}
/**
Saves the calculator history to a file.
*/
public void save()
{
try
{
ByteArrayOutputStream out =
new ByteArrayOutputStream();
PrintStream printOut = new PrintStream(out);
printOut.print(panel.getText());
InputStream data = new ByteArrayInputStream(
out.toByteArray());
FileSaveService service = (FileSaveService)
ServiceManager.lookup(
"javax.jnlp.FileSaveService");
service.saveFileDialog(".",
new String[] { "txt" }, data, "calc.txt");
}
catch (UnavailableServiceException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (IOException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
}
private CalculatorPanel panel;
}
/**
A panel with calculator buttons and a result display.
*/
|
用jbuilder,里面有一种叫XYlayout的布局,用了这种布局,就可以象其他可视化编程环境一样把控件拖来拖去,拖到自己想要的地方了。
good luck
good luck
|
java技术教程(基础篇)
|
applet默认的布局方式是FlowLayout,就是从左到右,从上到下的布局
|
用jbuilder吧,改一下布局就行了,哪有那么麻烦!
|
java 程序设计百事通 张洪斌 写的 很简单 比较适合入门.
|
jbuilder
|
用jbuilder吧
|
顶一下!!!