2012-04-15 38 views
8

Tôi đang cố gắng phát triển một tính năng giống như bánh mì nướng (Android) trong ứng dụng Swing của tôi. Là một độc lập, nó hoạt động hoàn hảo. Nhưng khi được tích hợp vào ứng dụng, các vấn đề đặt ra của nó.Android giống như bánh mì nướng trong Swing

Các tập tin Class là:

import java.awt.*; 
import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 
import java.awt.geom.RoundRectangle2D; 
import javax.swing.ImageIcon; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import net.mindcrew.utils.LayoutHelper.Packer; 

public class Toast extends JDialog { 

    String text; 

    public Toast(String text) { 
     this.text = text; 
     initComponents(); 
    } 

    private void initComponents(){ 
     setLayout(new GridBagLayout()); 
     addComponentListener(new ComponentAdapter() { 
      // Give the window an rounded rect shape. LOOKS GOOD 
      // If the window is resized, the shape is recalculated here. 
      @Override 
      public void componentResized(ComponentEvent e) { 
       setShape(new RoundRectangle2D.Double(0,0,getWidth(),getHeight(),50,50)); 
      } 
     }); 

     setUndecorated(true); 
     setSize(300,100); 
     setLocationRelativeTo(null); 
     getContentPane().setBackground(Color.BLACK); 

     // Determine what the GraphicsDevice can support. 
     GraphicsEnvironment ge = 
      GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice gd = ge.getDefaultScreenDevice(); 
     final boolean isTranslucencySupported = 
      gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT); 

     //If shaped windows aren't supported, exit. 
     if (!gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) { 
      System.err.println("Shaped windows are not supported"); 
     } 

     //If translucent windows aren't supported, 
     //create an opaque window. 
     if (!isTranslucencySupported) { 
      System.out.println(
       "Translucency is not supported, creating an opaque window"); 
     } 

     // Set the window to 70% translucency, if supported. 
     if (isTranslucencySupported) { 
      setOpacity(0.9f); 
     } 

     ImageIcon loading = new ImageIcon(Toast.class.getResource("/net/mindcrew/utils/userinterface/resources/loading-photo.gif")); 

     JLabel label = new JLabel(text); 
     label.setForeground(Color.WHITE); 
     label.setIcon(loading); 
     Packer packer = new Packer(this); 
     packer.pack(label).fillboth().west().inset(0, 50, 0, 20); 
    } 

    public static Toast showDailog(String textToDisplay){ 
     final Toast toast = new Toast(textToDisplay); 
     // Display the window. 
     Thread thread = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       toast.setVisible(true); 
      } 
     }); 
     thread.start(); 
     return toast; 
    } 

    @Override 
    public void hide(){ 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       setVisible(false); 
       dispose(); 
      } 
     }); 
    } 

    public static void main(String... args){ 
     Toast toast = Toast.showDailog("Display"); 
     try{ 
      Thread.sleep(5000); 
     } 
     catch (Exception e){} 
     toast.hide(); 
    } 

} 

Có thể có một số lỗi thực hiện, nhưng đây là điều cơ bản.

Tính năng này hoạt động tốt. Nhưng khi tôi cố gắng đưa nó vào con đường của một hoạt động chuyên sâu tài nguyên, nó bị vấp ngã. Như trong hoạt hình GIF không hiển thị, mà tôi nghĩ rằng nó ngụ ý rằng loại của nó bị đình trệ.

Việc sử dụng là:

Toast toast = Toast.showDailog("Generating PDF"); 

//resource intensive operation. Takes about 3-5seconds to execute 

    toast.hide(); 

Để thêm phần khốn khỗ, thậm chí sau khi "Toast" đã được xử lý, ứng dụng đang trở thành khiếp chậm. Tôi khá chắc chắn rằng chậm lại không phải là do các hoạt động trong câu hỏi, kể từ khi nó hoạt động hoàn hảo nếu tôi làm đi với "Bánh mì nướng".

Ai đó có thể vui lòng chỉ ra điều gì sai ở đây ???

Tôi đã xem qua this question. Nhưng những thứ có quá phức tạp hơn những gì tôi đang tìm kiếm. Những gì tôi đang tìm kiếm là một hộp thoại đơn giản. Không phải là một khung thổi đầy đủ mà cần phải chứa một số thành phần.

+0

* Trân trọng Binaek Sarkar Foundation httc: //www.foundation.....in/* Không bao gồm sigs. hoặc thẻ gọi điện trong bài đăng. Nếu thông tin quan trọng đối với bạn, hãy đặt nó vào [hồ sơ của bạn] (http://stackoverflow.com/users/951243/binaek-sarkar). –

+0

Rất tiếc! Sẽ nhớ nó lần sau. Cảm ơn bạn đã chỉ ra. –

+0

Tôi đã xóa nó trước khi bình luận. –

Trả lời

0

Có thể là JOptionPane là những gì bạn cần?

+0

Không thực sự. Tôi đang cố gắng tạo một hộp thoại tùy chỉnh. Những gì tôi đang cố gắng để tìm ra là một phiên bản Desktop của Android Toast và/hoặc iOS Growl. –

0

Tôi nghĩ rằng bạn có thể giải quyết vấn đề của bạn bằng cách không trọng dụng phương pháp ẩn trong Dialog và sửa đổi các phương pháp showDialog là một cái gì đó như thế này:

public static void showDailog(String textToDisplay){ 
    final Toast toast = new Toast(textToDisplay); 
    // Display the window. 
    Thread thread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try{ 
       toast.setVisible(true); 
       Thread.sleep(5000); 
       toast.setVisible(false); 
       toast.dispose(); 
      } 
      catch(Exception ex){ 
       ex.printStackTrace(); 
      } 
     } 
    }); 
    thread.start(); 
} 
0

Tôi đã thực hiện một số thay đổi mà làm việc cho tôi, hy vọng điều này sẽ hữu ích Android like Toast using Java Swing

+1

Nếu có thể làm cho câu trả lời hoàn thành trên riêng của họ, liên kết bên ngoài là tốt nhưng một câu trả lời sẽ có thể tồn tại mà không có nó –

6

Hãy thử mã này cho bánh mì nướng:

public class Test extends JFrame { 

    private JPanel contentPane; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Test frame = new Test(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public Test() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane); 

     JButton btnTestToast = new JButton("Test Toast"); 
     btnTestToast.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       ToastMessage toastMessage = new ToastMessage("Sample text to toast ",3000); 
       toastMessage.setVisible(true); 
      } 
     }); 
     contentPane.add(btnTestToast, BorderLayout.SOUTH); 
    } 

} 

public class ToastMessage extends JDialog { 
    int miliseconds; 
    public ToastMessage(String toastString, int time) { 
     this.miliseconds = time; 
     setUndecorated(true); 
     getContentPane().setLayout(new BorderLayout(0, 0)); 

     JPanel panel = new JPanel(); 
     panel.setBackground(Color.GRAY); 
     panel.setBorder(new LineBorder(Color.LIGHT_GRAY, 2)); 
     getContentPane().add(panel, BorderLayout.CENTER); 

     JLabel toastLabel = new JLabel(""); 
     toastLabel.setText(toastString); 
     toastLabel.setFont(new Font("Dialog", Font.BOLD, 12)); 
     toastLabel.setForeground(Color.WHITE); 

     setBounds(100, 100, toastLabel.getPreferredSize().width+20, 31); 


     setAlwaysOnTop(true); 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     int y = dim.height/2-getSize().height/2; 
     int half = y/2; 
     setLocation(dim.width/2-getSize().width/2, y+half); 
     panel.add(toastLabel); 
     setVisible(false); 

     new Thread(){ 
      public void run() { 
       try { 
        Thread.sleep(miliseconds); 
        dispose(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     }.start(); 
    } 
} 
1

tôi implemen ted nó tương tự như tooltip. Nó đã được thử nghiệm và làm việc.

import java.awt.Color; 
import java.awt.Label; 
import java.awt.Point; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JComponent; 
import javax.swing.Popup; 
import javax.swing.PopupFactory; 

public class Toast { 

    private final JComponent component; 
    private Point location; 
    private final String message; 
    private long duration; //in millisecond 

    public Toast(JComponent comp, Point toastLocation, String msg, long forDuration) { 
     this.component = comp; 
     this.location = toastLocation; 
     this.message = msg; 
     this.duration = forDuration; 

     if(this.component != null) 
     { 

      if(this.location == null) 
      { 
       this.location = component.getLocationOnScreen(); 
      } 

      new Thread(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        Popup view = null; 
        try 
        { 
         Label tip = new Label(message); 
         tip.setForeground(Color.red); 
         tip.setBackground(Color.white); 
         view = PopupFactory.getSharedInstance().getPopup(component, tip , location.x + 30, location.y + component.getHeight() + 5); 
         view.show(); 
         Thread.sleep(duration); 
        } catch (InterruptedException ex) 
        { 
         Logger.getLogger(Toast.class.getName()).log(Level.SEVERE, null, ex); 
        } 
        finally 
        { 
         view.hide(); 
        } 
       } 
      }).start(); 
     } 
    } 



    public static void showToast(JComponent component, String message) 
    { 
     new Toast(component, null, message, 2000/*Default 2 Sec*/); 
    } 

    public static void showToast(JComponent component, String message, Point location, long forDuration) 
    { 
     new Toast(component, location, message, forDuration); 
    } 
} 

Để sử dụng lớp này bạn chỉ cần gọi showToast()

0

Bạn có thể sử dụng một tròn tự xử lý JFrame như lớp phủ mà là vị trí tương ứng với cửa sổ ứng dụng của bạn. Bằng cách làm mờ nó trong một ra nó trông giống như một bánh mì nướng Android. Dưới đây là các mã:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.RoundRectangle2D; 

class Toast extends JFrame { 

    private final float MAX_OPACITY = 0.8f; 
    private final float OPACITY_INCREMENT = 0.05f; 
    private final int FADE_REFRESH_RATE = 20; 

    private final int WINDOW_RADIUS = 15; 
    private final int CHARACTER_LENGTH_MULTIPLIER = 9; 
    private final int DISTANCE_FROM_PARENT_BOTTOM = 100; 


    public Toast(JFrame owner, String toastText) { 
     setTitle("Transparent JFrame Demo"); 
     setLayout(new GridBagLayout()); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setUndecorated(true); 
     setFocusableWindowState(false); 

     setOpacity(0.4f); 


     // setup the toast lable 
     JLabel b1 = new JLabel(toastText); 
     b1.setForeground(Color.WHITE); 
     b1.setOpaque(false); 
     add(b1); 

     setSize(toastText.length() * CHARACTER_LENGTH_MULTIPLIER, 50); 

     int x = (int) (owner.getLocation().getX() + (owner.getWidth()/2)); 
     int y = (int) (owner.getLocation().getY() + owner.getHeight() - DISTANCE_FROM_PARENT_BOTTOM); 
     setLocation(new Point(x, y)); 


     // configure frame 
     setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), WINDOW_RADIUS, WINDOW_RADIUS)); 
     getContentPane().setBackground(new Color(0, 0, 0, 170)); 

    } 


    public void fadeIn() { 
     setOpacity(0); 
     setVisible(true); 

     final Timer timer = new Timer(FADE_REFRESH_RATE, null); 
     timer.setRepeats(true); 
     timer.addActionListener(new ActionListener() { 
      private float opacity = 0; 


      @Override 
      public void actionPerformed(ActionEvent e) { 
       opacity += OPACITY_INCREMENT; 
       setOpacity(Math.min(opacity, MAX_OPACITY)); 
       if (opacity >= MAX_OPACITY) { 
        timer.stop(); 
       } 
      } 
     }); 

     timer.start(); 
    } 


    public void fadeOut() { 
     final Timer timer = new Timer(FADE_REFRESH_RATE, null); 
     timer.setRepeats(true); 
     timer.addActionListener(new ActionListener() { 
      private float opacity = MAX_OPACITY; 


      @Override 
      public void actionPerformed(ActionEvent e) { 
       opacity -= OPACITY_INCREMENT; 
       setOpacity(Math.max(opacity, 0)); 
       if (opacity <= 0) { 
        timer.stop(); 
        setVisible(false); 
        dispose(); 
       } 
      } 
     }); 

     setOpacity(MAX_OPACITY); 
     timer.start(); 
    } 


    public static void makeToast(final JFrame owner, final String toastText, final int durationSec) { 


     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        Toast toastFrame = new Toast(owner, toastText); 
        toastFrame.fadeIn(); 
        Thread.sleep(durationSec * 1000); 
        toastFrame.fadeOut(); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
      } 
     }).start(); 


    } 


    public static void main(String args[]) { 
     final JFrame frame = new JFrame("Cloud Tester"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     JPanel jPanel = new JPanel(); 
     jPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     JButton toastButton = new JButton("show toast"); 
     jPanel.add(toastButton); 

     toastButton.addActionListener(new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       Toast.makeToast(frame, "a toast!", 3); 
      } 
     }); 


     frame.add(jPanel); 
     frame.setSize(800, 600); 
     frame.setVisible(true); 
    } 
} 
Các vấn đề liên quan