//提示:坐标依次打印在命令符窗口
//提示:坐标依次打印在命令符窗口
//提示:坐标依次打印在命令符窗口
//不就是监听鼠标事件吗?
import javaxswing;
import javaawt;
import javaawtevent;
import javaxswingborderBorder;
/
我想建立个界面,可以加载本机中。
加载后可以通过鼠标点击获得上任意点坐标。
提问者: sunny929929 - 试用期 一级
/
public class MyPicture extends JFrame implements MouseListener{
private JLabel tipLabel;
/
main()
/
public static void main(String[] args){
MyPicture frame = new MyPicture();
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
/
constructor
/
public MyPicture(){
setSize(800, 600);//根据要求调整大小
setLocation(100,100);
setTitle("获得上任意点坐标");
setResizable(false);
Container con=getContentPane();
ImageIcon bgIcon=new ImageIcon("bgpicjpg");//注意的路径
ImagePanel backpicPanel=new ImagePanel(bgIcon);
backpicPaneladdMouseListener(this);
conadd(backpicPanel,BorderLayoutCENTER);
tipLabel=new JLabel("--------------------提示:坐标依次打印在屏幕上!--------------------");
conadd(tipLabel,BorderLayoutSOUTH);
}
/
/
public void mousePressed(MouseEvent e){
int x=egetX();
int y=egetY();
String message="("+x+","+y+")";
tipLabelsetText(message);
Systemoutprintln(message);
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
}
/
类ImagePanel,用于添加背景
/
class ImagePanel extends JPanel{
private Image img;
public ImagePanel (ImageIcon imageIcon){
img=imageIcongetImage();
}
public void paintComponent(Graphics g){
superpaintComponent(g);
gdrawImage(img,0,0,this);
}
}
可以新建个面板,在面板里放入带的JLabel,填满面板即可。
JPanel jp = new JPanel(); //新建面板
jpsetLayout(new FlowLayout()); //设置面板布局
ImageIcon ii=new ImageIcon(getClass()getResource("/Picture/ijpg"));
JLabel uppicture=new JLabel(ii); //往面板里加入JLabel
thissetVisible(true);
运行如图
参考代码如下
import javaawt;import javaawtevent;
import javaxswing;
public class RegDemo extends JFrame implements ActionListener{
JTextField jtf;
JPasswordField jpf;
public RegDemo() {
//组件的创建, 和布局安排
JPanel jpc = new JPanel();//默认流式布局
JPanel jp1 = new JPanel(new GridLayout(2, 2,5,10));//网格布局
jp1setBorder(BorderFactorycreateTitledBorder("用户注册"));
JLabel jl1 = new JLabel("用户名:");
jtf = new JTextField(10);
JLabel jl2 = new JLabel("密码:");
jpf = new JPasswordField(10);
jpfsetEchoChar('');//用号来隐藏密码的显示
jp1add(jl1);jp1add(jtf);
jp1add(jl2);jp1add(jpf);
jpcadd(jp1);
add(jpc);
JButton jb1 = new JButton("提交");
jb1addActionListener(this);
jb1setActionCommand("yes");
JButton jb2 = new JButton("取消");
jb2addActionListener(this);
jb2setActionCommand("no");
JPanel jp2 = new JPanel();
jp2add(jb1);jp2add(jb2);
add(jp2,BorderLayoutSOUTH);
setTitle("用户注册界面");
setSize(280, 280);
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//
setVisible(true);
}
public static void main(String[] args) {
new RegDemo();
}
@Override
public void actionPerformed(ActionEvent e) {
if(egetActionCommand()equals("yes")){
String name = jtfgetText()trim();
String pwd = new String(jpfgetPassword());
if(nameequals("")||pwdequals("")){
JOptionPaneshowMessageDialog(this, "你还没有输入用户名或者密码");
}else{
JOptionPaneshowMessageDialog(this, "注册成功!用户名"+name+",密码"+pwd);
}
}else{
jtfsetText("");
jpfsetText("");
}
}
}
按照你的要求编写的Java程序如下:
import javaawtBorderLayout;import javaawtColor;
import javaawtGraphics;
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventItemEvent;
import javaawteventItemListener;
import javaawteventMouseEvent;
import javaawteventMouseListener;
import javaawteventMouseMotionListener;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioObjectInputStream;
import javaioObjectOutputStream;
import javaioSerializable;
import javautilArrayList;
import javautilList;
import javaxswingButtonGroup;
import javaxswingJButton;
import javaxswingJColorChooser;
import javaxswingJFileChooser;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJRadioButton;
import javaxswingfilechooserFileNameExtensionFilter;
public class CAA extends JFrame implements ActionListener,ItemListener{
List<PainterObject> arraylist=new ArrayList<PainterObject>();
JRadioButton jrb1=new JRadioButton("Line",true);
JRadioButton jrb2=new JRadioButton("Rect");
JRadioButton jrb3=new JRadioButton("RoundRect");
JRadioButton jrb4=new JRadioButton("FillRect");
ButtonGroup bg=new ButtonGroup();
JButton jb1=new JButton("Color Chooser");
JButton jb2=new JButton("Save");
JButton jb3=new JButton("Load");
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
PainterJPanel pjp=new PainterJPanel();
int type=1;//type=1画线2画长方形3画圆角长方形4画填充长方形
Color c;
String saveType[] = {"obj"}; //存储文件的扩展名
CAA(){
super("Painter");
bgadd(jrb1);bgadd(jrb2);bgadd(jrb3);bgadd(jrb4);
jp1setLayout(new GridLayout(2,2));
jp1add(jrb1);jp1add(jrb2);jp1add(jrb3);jp1add(jrb4);
jp2setLayout(new GridLayout(1,3));
jp2add(jb1);jp2add(jb2);jp2add(jb3);
jrb1addItemListener(this);
jrb2addItemListener(this);
jrb3addItemListener(this);
jrb4addItemListener(this);
jb1addActionListener(this);
jb2addActionListener(this);
jb3addActionListener(this);
add(jp1,BorderLayoutNORTH);
add(jp2,BorderLayoutSOUTH);
add(pjp,BorderLayoutCENTER);
setSize(500, 500);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if(aegetSource()==jb1){
c = JColorChoosershowDialog(this, "Choose a Color",pjpgetForeground());
if (c != null)
pjpsetForeground(c);
}
if(aegetSource()==jb2){
JFileChooser fd = new JFileChooser();
fdsetFileFilter(new FileNameExtensionFilter("obj", saveType));
fdshowSaveDialog(this);
File f = fdgetSelectedFile();
if(f != null){
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f+"obj"));
outwriteObject(arraylist);
outclose();
}catch(Exception e){
egetStackTrace();
}
}
}
if(aegetSource()==jb3){
JFileChooser fd = new JFileChooser();
fdsetFileFilter(new FileNameExtensionFilter("obj", saveType));
fdshowOpenDialog(this);
File f = fdgetSelectedFile();
if(f != null){
try{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
arraylist=(ArrayList)inreadObject();
pjprepaint();
inclose();
}catch(Exception e){
egetStackTrace();
}
}
}
}
@Override
public void itemStateChanged(ItemEvent ie) {
if(iegetStateChange()==ItemEventSELECTED){
if(iegetItem()==jrb1){
type=1;
}
if(iegetItem()==jrb2){
type=2;
}
if(iegetItem()==jrb3){
type=3;
}
if(iegetItem()==jrb4){
type=4;
}
}
}
public static void main(String[] args) {
new CAA();
}
class PainterJPanel extends JPanel implements MouseListener,MouseMotionListener{
int x,y;
int preparex,preparey;
PainterPoint pPointStart;
PainterPoint pPointEnd;
PainterJPanel(){
setOpaque(true);
setBackground(ColorWHITE);
thisaddMouseListener(this);
thisaddMouseMotionListener(this);
}
public void paint(Graphics g){
superpaint(g);
for(int i=0;i<arraylistsize();i++){
PainterObject pObject=(PainterObject)arraylistget(i);
gsetColor(pObjectgetColor());
int x1=pObjectgetPpStart()getX();
int y1=pObjectgetPpStart()getY();
int x2=pObjectgetPpEnd()getX();
int y2=pObjectgetPpEnd()getY();
switch(pObjectgetType()){
case 1:gdrawLine(x1,y1,x2,y2);break;
case 2:gdrawRect(x1, y1, Mathabs(x2-x1), Mathabs(y2-y1));break;
case 3:gdrawRoundRect(x1, y1, Mathabs(x2-x1), Mathabs(y2-y1),20,20);break;
case 4:gfillRect(x1, y1, Mathabs(x2-x1), Mathabs(y2-y1));break;
}
}
gsetColor(c);
switch(type){
case 1:gdrawLine(x, y, preparex, preparey);break;
case 2:gdrawRect(x, y, Mathabs(preparex-x), Mathabs(preparey-y));break;
case 3:gdrawRoundRect(x, y, Mathabs(preparex-x), Mathabs(preparey-y),20,20);break;
case 4:gfillRect(x, y, Mathabs(preparex-x), Mathabs(preparey-y));break;
}
}
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent arg0) {}
@Override
public void mouseExited(MouseEvent arg0) {}
@Override
public void mousePressed(MouseEvent me) {
x=megetX();
y=megetY();
pPointStart=new PainterPoint();
pPointStartsetX(x);
pPointStartsetY(y);
}
@Override
public void mouseReleased(MouseEvent me) {
x=megetX();
y=megetY();
pPointEnd=new PainterPoint();
pPointEndsetX(x);
pPointEndsetY(y);
PainterObject po=new PainterObject();
posetPpStart(pPointStart);
posetPpEnd(pPointEnd);
posetType(type);
posetColor(c);
arraylistadd(po);
repaint();
}
@Override
public void mouseDragged(MouseEvent me) {
preparex=megetX();
preparey=megetY();
repaint();
}
@Override
public void mouseMoved(MouseEvent arg0) {}
}
}
class PainterObject implements Serializable{
PainterPoint ppStart;
PainterPoint ppEnd;
int type;
Color color;
public PainterPoint getPpStart() {
return ppStart;
}
public void setPpStart(PainterPoint ppStart) {
thisppStart = ppStart;
}
public PainterPoint getPpEnd() {
return ppEnd;
}
public void setPpEnd(PainterPoint ppEnd) {
thisppEnd = ppEnd;
}
public int getType() {
return type;
}
public void setType(int type) {
thistype = type;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
thiscolor = color;
}
}
class PainterPoint implements Serializable{
int x;
int y;
public int getX() {
return x;
}
public void setX(int x) {
thisx = x;
}
public int getY() {
return y;
}
public void setY(int y) {
thisy = y;
}
}
运行结果:
以上就是关于用java怎么获得一张图片上的一个文字的坐标点 求高手解答全部的内容,包括:用java怎么获得一张图片上的一个文字的坐标点 求高手解答、怎么在Java里添加背景图片、Java编写图形用户界面程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)